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.
How to print or grep only the matched string or text or word or pattern in Linux using bash
Reviewed by Deepak Prasad
on
Tuesday, December 12, 2017
Rating:
No comments: