Reputation: 69
This is the text:
We will now present the Linux ls command
... here description of ls
We will now present the Linux cd command
... here description of cd
... more description
Done
This following sed replacement regex is applied on the text
sed 's/.*Linux \(.*\) .*/\1:/' ex2.txt
Which gives the following output
ls:
... here description of ls
cd:
... here description of cd
... more description
Done
Can someone please walk me through on how it worked?
Upvotes: 0
Views: 104
Reputation: 19485
.*Linux
: Anything before the word “Linux” and that word itself followed by a space. (We will now present the Linux
)
\(.*\)
: Then something captured with parentheses. (ls
or cd
)
.*
: Then another space and anything else. ( command
)
Replaced by:
\1:
: Whatever the first capturing group has captured, followed by a colon (ls:
or cd:
)
In this case, .
matches any character except line breaks.
\(.*\)
matches only that one word because there has to be a space before “Linux” and there has to be a space after that word: the space between the command name and the word “command”.
The parentheses don’t have to be escaped with a backslash if extended regex is used (sed -r
…).
So you could also write:
sed -r 's/.*Linux (.*) .*/\1:/' ex2.txt
Upvotes: 3