By default when we use grep without any switch it prints the entire line which matches the string we tried to grep for
For eg
I would like search for "ModLoad" in my rsyslog.conf file
So when attempted without any switch or argument with grep
So I get a whole bunch of stuff there but what if I would like to grep only the text "ModLoad" and nothing else
Here I have switched to egrep as now we plan on using regex which works well with egrep or you can use "grep -E"
I hope the article was useful.
For eg
I would like search for "ModLoad" in my rsyslog.conf file
So when attempted without any switch or argument with grep
# grep ModLoad /etc/rsyslog.conf
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
$ModLoad imklog # reads kernel messages (the same are read from journald)
$ModLoad immark # provides --MARK-- message capability
$ModLoad imudp
$ModLoad imtcp
So I get a whole bunch of stuff there but what if I would like to grep only the text "ModLoad" and nothing else
# egrep -o ModLoad /etc/rsyslog.conf
ModLoad
ModLoad
ModLoad
ModLoad
ModLoad
ModLoad
Here I have switched to egrep as now we plan on using regex which works well with egrep or you can use "grep -E"
Here -o means "Print each match, but only the match, not the entire line."
I hope the article was useful.
Follow the below links for more tutorials
10 Basic examples of SED to perform per line action (print/delete)
sed: perform search and replace only on specific lines (not globally)
sed: delete all blank lines from a text file
sed: case insensitive actions (search replace delete..)
sed: remove all leading and ending blank whitespace from a file
sed :Replace whole line when match found
sed: Replace string based on line number (certain line)
sed: Insert multiple lines before or after pattern match
sed: match string and append new line (before or after)
How to add line number at the beginning of each line
sed: Insert character in the beginning or end of line with matched pattern
sed: insert word after match in middle of the line
sed: perform search and replace only on specific lines (not globally)
sed: delete all blank lines from a text file
sed: case insensitive actions (search replace delete..)
sed: remove all leading and ending blank whitespace from a file
sed :Replace whole line when match found
sed: Replace string based on line number (certain line)
sed: Insert multiple lines before or after pattern match
sed: match string and append new line (before or after)
How to add line number at the beginning of each line
sed: Insert character in the beginning or end of line with matched pattern
sed: insert word after match in middle of the line