grep
command is used to extract lines of data from files or extract data from other Linux commands by using a pipe. When using the Linux grep
command, there are many ways you can extract or print lines. You can use standard text to grep
or you can use regex patterns. When using regex patterns you can use basic regular expression (BRE), extended regular expression (ERE), or even a Perl regular expression!There are many different
grep
options you can use in the grep
syntax. I myself only use a few options with grep
on a regular basis. The basic grep
syntax is grep [options] [pattern] [file|files]
.grep
quick reference guide:You can use
grep -R
, grep -r
, grep --recursive
, which will allow you to have grep
parse files recursivley into other directories.If you are working with code, or just want the output lines to be numbered you can use
grep -n
or grep --line-number
.Another option I use on a daily basis with
grep
is grep -i
or grep --ignore-case
, which will ignore case on both the input file, and the pattern. by default grep
is case sensitive, so you have to use this option if you do not want it case sensitive.If you are wanting to extract multiple patterns from your file, you can use
grep -e
which can be used multiple times to extract multiple patterns from your file. So if you are wanting to grep
two different strings from one file in Linux, you could do grep -e firstpattern -e secondpattern /home/$USER/file.txt
.Here are some
grep
examples of using the Linux grep
command:If I had a plain text file called
phone-book.txt
, that had phone numbers listed in this order: First Name Last Name - Street Address - Phone Number
Now I want to get Brandon Stimmel’s phone number. I could run
grep Brandon\ Stimmel phone-book.txt
and grep
would print Brandon Stimmel - 100101 Digital Ave. Tech, Ohio 44333 (330) 222-7222
. Notice I used Brandon\ Stimmel
, I did this because you can not use a space on the command line, or it will be parsed as the next section of the command, which grep Brandon Stimmel phone-book.txt
without the \
would try to extract Brandon from the file Stimmel, which doesn’t exist. So remember to escape your spaces with a \
if you are using them in your pattern/search string.For another example, if I want to bring up who owns the phone number (330) 222-7222 as it showed up on my caller ID, but I forgot who’s phone number it was, I can do
grep \(330\)\ 222-7222 phone-book.txt
which would again display: Brandon Stimmel - 100101 Digital Ave. Tech, Ohio 44333 (330) 222-7222
.You can also
grep
for just the last name, say if you are having a family reunion and you want to bring up all of the people with the last name of Stimmel. grep Stimmel phone-book.txt
this will bring up every person in phone-book.txt
that has the last name of Stimmel.You can also pipe data to
grep
or pipe data from grep
to use it in a bash script. Say if you are wanting to do the above example that you found a phone number and forgot who’s it was, but you don’t want to show the entire line with name, address, and number, you just want the name and only the name. grep \(330\)\ 222-7222 phone-book.txt|awk '{print $1" "$2}'
which will output Brandon Stimmel
. The grep
part of this code will output the full string, then we pass that data onto awk
which we used it to print only the first and second fields.Well that’s it for the
grep
tutorial, for now anyways. As always, thank you for reading our Linux Tutorials and I hope you find these Linux Tutorials very helpful! Remember to bookmark Beginner Linux Tutorial so you can always come back and find more helpful Linux Tutorials!
No comments:
Post a Comment