
How do I exploit grep command on Linux or Apple macOS/OS X? How can I exploit grep command on Unix working programs? Are you able to give me a easy examples of the grep command?
The grep command is used to go looking textual content. It searches the given file for strains containing a match to the given strings or phrases. It is likely one of the most helpful instructions on Linux and Unix-like system. Allow us to see the best way to use grep on a Linux or Unix like system.
Do you know?
The title, “grep”, derives from the command used to carry out an identical operation, utilizing the Unix/Linux textual content editor ed:
g/re/p
The grep utilities are a household that features grep, egrep, and fgrep for looking out duties. For many makes use of, you must use fgrep because it the quickest and solely look into strings and phrases. Nonetheless, typing grep is straightforward. Therefore, it’s a private selection.
grep command examples in Linux and Unix
Under is a few normal grep command defined with examples to get you began with grep on Linux, macOS, and Unix:
- Search any line that comprises the phrase in filename on Linux: grep ‘phrase’ filename
- Carry out a case-insensitive seek for the phrase ‘bar’ in Linux and Unix: grep -i ‘bar’ file1
- Search for all information within the present listing and in all of its subdirectories in Linux for the phrase ‘httpd’grep -R ‘httpd’ .
- Search and show the entire variety of occasions that the string ‘nixcraft’ seems in a file named frontpage.md: grep -c ‘nixcraft’ frontpage.md
Allow us to see all instructions and choices in particulars.
Syntax
The syntax is as follows:
grep ‘phrase’ filename fgrep ‘word-to-search’ file.txt grep ‘phrase’ file1 file2 file3 grep ‘string1 string2’ filename cat otherfile | grep ‘one thing’ command | grep ‘one thing’ command option1 | grep ‘knowledge’ grep –color ‘knowledge’ fileName grep [-options] sample filename fgrep [-options] phrases file |
How do I exploit grep to go looking a file on Linux?
Search /and so forth/passwd file for boo consumer, enter:
grep boo /and so forth/passwd
Pattern outputs:
foo:x:1000:1000:boo,,,:/residence/boo:/bin/ksh
We will use fgrep/grep to search out all of the strains of a file that include a specific phrase. For instance, to checklist all of the strains of a file named handle.txt within the present listing that include the phrase “California”, run:
fgrep California handle.txt
Please be aware that the above command additionally returns strains the place “California” is a part of different phrases, corresponding to “Californication” or “Californian”. Therefore go the -w choice with the grep/fgrep command to get solely strains the place “California” is included as a complete phrase:
fgrep -w California handle.txt
You possibly can drive grep to disregard phrase case i.e match boo, Boo, BOO and all different mixture with the -i choice. As an example, sort the next command:
grep -i “boo” /and so forth/passwd
The final grep -i “boo” /and so forth/passwd can run as follows utilizing the cat command too:
cat /and so forth/passwd | grep -i “boo”
The right way to use grep recursively
You possibly can search recursively i.e. learn all information beneath every listing for a string “192.168.1.5”
$ grep -r “192.168.1.5” /and so forth/
OR
$ grep -R “192.168.1.5” /and so forth/
Pattern outputs:
/and so forth/ppp/choices:# ms-wins 192.168.1.50
/and so forth/ppp/choices:# ms-wins 192.168.1.51
/and so forth/NetworkManager/system-connections/Wired connection 1:addresses1=192.168.1.5;24;192.168.1.2;
You will notice end result for 192.168.1.5 on a separate line preceded by the title of the file (corresponding to /and so forth/ppp/choices) during which it was discovered. The inclusion of the file names within the output knowledge could be suppressed by utilizing the -h choice as follows:
$ grep -h -R “192.168.1.5” /and so forth/
OR
$ grep -hR “192.168.1.5” /and so forth/
Pattern outputs:
# ms-wins 192.168.1.50
# ms-wins 192.168.1.51
addresses1=192.168.1.5;24;192.168.1.2;
The right way to use grep to go looking phrases solely
If you seek for boo, grep will match fooboo, boo123, barfoo35 and extra. You possibly can drive the grep command to pick out solely these strains containing matches that kind complete phrases i.e. match solely boo phrase:
$ grep -w “boo” file
The right way to use grep to go looking 2 totally different phrases
Use the egrep command as follows:
$ egrep -w ‘word1|word2’ /path/to/file
How can I depend line when phrases has been matched
The grep can report the variety of occasions that the sample has been matched for every file utilizing -c (depend) choice:
$ grep -c ‘phrase’ /path/to/file
Go the -n choice to precede every line of output with the variety of the road within the textual content file from which it was obtained:
$ grep -n ‘root’ /and so forth/passwd
1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/residence/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/residence/initroot:/bin/ksh
Pressure grep invert match
You should utilize -v choice to print inverts the match; that’s, it matches solely these strains that don’t include the given phrase. For instance print all line that don’t include the phrase bar:
$ grep -v bar /path/to/file
Show strains earlier than and after the match
Wish to see the strains earlier than your matches? Strive passing the -B to the grep:
grep -B NUM “phrase” file
grep -B 3 “foo” file1
Equally, show the strains after your matches by passing the -A to the grep:
grep -A NUM “string” /pth/to/file
grep -A 4 “dropped” /var/log/ufw.log
We will mix these two choices to get most significant outputs:
grep -C 4 -B 5 -A 6 –color ‘error-code’ /var/log/httpd/access_log
Here’s a pattern shell script that fetches the Linux kernel obtain urls:
……. … _out=”/tmp/out.$$” curl -s https://www.kernel.org/ > “$_out” ####################### ## grep -A used right here ## ####################### url=”$(grep -A 2 ‘ |
‘ ${_out} | grep -Eo ‘(http|https)://[^/”]+.*xz’)” gpgurl=”$” notify-send “A brand new kernel model ($distant) has been launched.” echo “* Downloading the Linux kernel (new model) …” wget -qc “$url” -O “${dldir}/${file}” wget -qc “$gpgurl” -O “${dldir}/${gpgurl##*/}” ….. .. |
‘ ${_out} | grep -Eo ‘(http|https)://[^/”]+.*xz’)”
gpgurl=”$”
notify-send “A brand new kernel model ($distant) has been launched.”
echo “* Downloading the Linux kernel (new model) …”
wget -qc “$url” -O “${dldir}/${file}”
wget -qc “$gpgurl” -O “${dldir}/${gpgurl##*/}”
…..
..
UNIX / Linux pipes
grep command typically used with shell pipes. On this instance, present the title of the arduous disk gadgets:
# dmesg | egrep ‘(s|h)d[a-z]’
Show cpu mannequin title:
# cat /proc/cpuinfo | grep -i ‘Mannequin’
Nonetheless, above command could be additionally used as follows with out shell pipe:
# grep -i ‘Mannequin’ /proc/cpuinfo
mannequin : 30 mannequin title : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz mannequin : 30 mannequin title : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz |
One in every of my favourite utilization of grep or egrep command to filter the output of the yum command/dpkg command/apt command/apt-get command:
dpkg –list | grep linux-image
yum search php | grep gd
apt search maria | egrep ‘server|shopper’
Linux grep instructions defined with shell pipes examples
How do I checklist simply the names of matching information?
Use the -l choice to checklist file title whose contents point out essential():
$ grep -l ‘essential’ *.c
Lastly, we will drive grep to show output in colours, enter:
$ grep –color vivek /and so forth/passwd
Grep command in motion
In conclusion, the –color choice enhance readblity. For instance, the GREP_COLOR setting variable and the grep –color=at all times can be utilized as follows:
GREP_COLOR=’1;35′ grep –color=at all times ‘vivek’ /and so forth/passwd
GREP_COLOR=’1;32′ grep –color=at all times ‘vivek’ /and so forth/passwd
GREP_COLOR=’1;37′ grep –color=at all times ‘root’ /and so forth/passwd
GREP_COLOR=’1;36′ grep –color=at all times no person /and so forth/passwd
As well as, to default pink colour now we will outline colours utilizing GREP_COLOR shell variable. The differnt colour helps us massivly with visible grepping.
Conclusion
The grep command is a really versatile and lots of new Linux or Unix customers discover it sophisticated. Therefore, I counsel you learn the grep man web page too. Allow us to summarize most import choices:
Linux grep command choices | Description |
---|---|
-i | Ignore case distinctions on Linux and Unix |
-w | Pressure PATTERN to match solely complete phrases |
-v | Choose non-matching strains |
-n | Print line quantity with output strains |
-h | Suppress the Unix file title prefix on output |
-r | Search directories recursivly on Linux |
-R | Similar to -r however comply with all symlinks |
-l | Print solely names of FILEs with chosen strains |
-c | Print solely a depend of chosen strains per FILE |
–color | Show matched sample in colours |
In the event you loved the grep tutorial, then you definitely may wish to learn our “Common Expressions in Grep” tutorial.
ADVERTISEMENTS
grep command to find a file in a directory,grep command in unix with example pdf,grep command in linux with examples,sed command in unix,grep command options,find and grep command in unix,awk command in unix,grep in current directory and subdirectories