LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   understanding sed patterns (https://www.linuxquestions.org/questions/linux-newbie-8/understanding-sed-patterns-4175621184/)

vincix 01-08-2018 04:56 AM

understanding sed patterns
 
How are patterns in sed handled in this situation?

source: http://gilmation.com/articles/regexp...attern-in-sed/
The following example finds words surrounded by the same number (ab is surrounded by 1 and ef is surrounded by 82) and removes the surrounding number:

Code:

$ echo "4 9 1 ab 1 cd 13 82 ef 82" | sed 's/\([0-9]*\) \([a-z]*\) \1/\2 /g'
Code:

4 9 ab cd 13 ef

Turbocapitalist 01-08-2018 05:01 AM

The substitute command is s/// as in

Code:

s/old/new/
So the \1 you have in your formula above is in the 'old' part. The 'new' part has only \2

So if you have a pattern along the lines of 'A B A' then it is replaced with 'B'

Turbocapitalist 01-08-2018 05:02 AM

Hey, don't delete stuff. Someone searching for a similar problem won't see the key part when this thread turns up in their search results.

vincix 01-08-2018 05:04 AM

Sorry, I didn't know how to handle it :) It was actually much easier than I had thought and I deleted it before you replied, otherwise I wouldn't have deleted it. I'll just rewrite the post then.

Turbocapitalist 01-08-2018 05:15 AM

Thanks.

vincix 01-08-2018 05:19 AM

I had interpreted the slash between \1 and \2 as the third occurrence, because I hadn't seen \1 in the first part of the substitution before, and I automatically thought that there was a another slash before \1. Thanks.


All times are GMT -5. The time now is 03:49 PM.