Unable to use end-of-line specification using $ symbol in grep

Asked 1 years ago, Updated 1 years ago, 71 views

Prerequisites/What you want to achieve
I am learning regular expressions in bash on macOS.
I tried to end the line with the $ symbol in grep, but the search pattern was not recognized.

problems you are having
The specified zen.txt file contains a line containing the string idea. but is not recognized when $ is added.

name-no-MacBook-Pro:Desktop name $grep idea.$zen.txt
name-no-MacBook-Pro —Desktop name$

Affected Source Code
macOS bash

name-no-MacBook-Pro:~name$exportGREP_OPTIONS='--color=always'
name-no-MacBook-Pro:~name$cd Desktop
name-no-MacBook-Pro —Desktop name $grep idea.$zen.txt
name-no-MacBook-Pro —Desktop name$

Tried

name-no-MacBook-Pro:Desktop name $grep idea.zen.txt
If the implementation is hard to explain, it's bad idea.\
If the implementation is easy to explain, it may be a good idea.\
Namespaces are one honking great idea --let's do more of these!}

Supplementary information (for example, FW/Tool version)
Terminal version 2.9.5 (421.2)

name-no-MacBook-Pro:Desktop name $catzen.txt
{\rtf1\ansi\ansicpg932\cocoartf1671\cocoasubrtf600
{\fontbl\f0\fnil\fcharset0Menlo-Regular;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
{\*\expandedcolortbl;;\csgray\c0;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

\f0\fs22\cf2\CocoaLigature0 The Zen of Python, by Tim Peters\
\
Beautiful is better than always.
Explicit is better than explicit.
Simple is better than complex.\
Complex is better than complicated.
Flat is better than nested.\
Sparse is better than dense.
Readability counts.\
Special cases areren't special enough to break the rules.\
Altough practicality beats security.
Errors should never pass silently.\
Unexplicitly silenced.\
In the face of Ambiguity, refresh the test to guide.
There should be one -- and preferably only one -- obvious way to do it.\
Altough that way may not be at first you're Dutch.
Now is better than never.
Altough never is off better than *right* now.\
If the implementation is hard to explain, it's bad idea.\
If the implementation is easy to explain, it may be a good idea.\
Namespaces are one honking great idea -- let's do more of these!\
}

macos bash grep

2022-09-30 21:43

1 Answers

Try connecting the patterns to a single quote.

 grep'idea.$'zen.txt

Naked $ is interpreted by the shell, so in such cases, quote it.

(Additional)
If \ is included at the end of the line, try escaping as follows:

 grep'idea.\\$'zen.txt


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.