Find the line number which contains the pattern using custom regex delimiter

As in example I’m trying to get line numbers which contains the pattern. My pattern contains slashes so I wanted to add custom delimiter.

This simple one works:

sed -n '/file/=' temp.txt

Using delimiter for string replace works too:

sed 's|file|gile|' temp.txt

but when I want to add delimiter to first example it doesn’t:

sed -n '|file /etc|=' temp.txt

I know I can escape slashes but I would prefer to add custom delimiter. Any idea how to fix my command?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Stéphane gave you the sed solution:

sed -n  '|file /etc|=' file

If you’re open to using other tools, you can also do

grep -n 'file /etc' file

That will also print the line itself, to get the line number alone try:

grep -n 'file /etc' file | cut -d: -f 1

Or, you can use perl:

perl -lne 'm|file /etc| && print $.' file

Or awk:

awk '$0 ~ "file /etc" {print NR}'

Method 2

In all context addresses, you have to escape the opening delimiter, unless you’re using the default /. Any following occurrences that are escaped are treated as the literal character, not as the ending delimiter.

  • default delimiter:
    /start/,/end/{/pattern/d;}
  • custom delimiter:
    #start#,#end#{#pattern#d;}

See the POSIX docs:

In a context address, the construction cREc where c is any character
other than a backslash or newline character, is identical to /RE/ If
the character designated by c appears following a backslash, then it
is considered to be that literal character, which does not terminate
the RE. For example, in the context address xabcxdefx, the second x
stands for itself, so that the regular expression is abcxdef.

Similar description in GNU sed man page:

/regexp/
       Match lines matching the regular expression regexp.      
cregexpc
       Match lines matching the regular expression regexp.  
       The c may be any character.

and FreeBSD sed man page:

In a context address, any character other than a backslash (``'')
or newline character may be used to delimit the regular expression.
The opening delimiter   needs to be preceded by a backslash unless it
is a slash.  For example, the   context address xabcx is equivalent
to /abc/.  Also, putting a backslash character before   the delimiting
character within the regular expression causes the character to be
treated literally.  For example, in the context address xabcxdefx,
the RE delimiter is an ``x'' and the second ``x'' stands for itself,
so that the regular expression is ``abcxdef''.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x