Home > other >  Trying to find the lines that start with \1 and ends with $ using grep
Trying to find the lines that start with \1 and ends with $ using grep

Time:05-03

Command used: grep '^\1.*$$' data.txt

Error given: grep: Invalid back reference

Contents of file:

\1 this is a line where search happens$
2 this is also an example$
3 STA
\1VLSI$
\2LIVS$
\1Static timing analuysisi$
\1sedawkgrep$
\1clocktreesynthesis
\1Andhra Pradesh$
\1Karnataka$

I need to print lines that start with \1 and end with $.

CodePudding user response:

In regular expressions, the \ character followed by a digit is a backreference to a previously-matched pattern in a group, which is why you're getting the error.

To make things more complicated, the $ is also a special character, meaning end-of-line. Both of these must be escaped with another backslash so that they don't have their symbolic meanings:

grep '^\\1.*\$$' data.txt

The above means: match beginning of line, followed by a literal backslash, followed by the digit 1, then zero or more of any characters until the literal $, then the end of line.

  • Related