Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=='''Hack 17. Grep Command'''== grep command is used to search files for a specific text. This is incredibly powerful command with lot of options. Syntax: grep <nowiki>[options] </nowiki>pattern <nowiki>[files]</nowiki> '''How can I find all lines matching a specific keyword on a file?''' In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines. <nowiki># </nowiki>grep John /etc/passwd jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash Option -v, will display all the lines except the match. In the example below, it displays all the records from /etc/password that doesn't match John. '''Note: '''There are several lines in the /etc/password that doesnโt contain the''' '''word John. Only the first line of the output is shown below. <nowiki># </nowiki>grep -v John /etc/passwd jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash '''How many lines matched the text pattern in a particular file?''' In the example below, it displays the total number of lines that contains the text John in /etc/passwd file. <nowiki># grep -c John /etc/passwd 2</nowiki> You can also get the total number of lines that did not match the specific pattern by passing option -cv. <nowiki># grep -cv John /etc/passwd 39</nowiki> '''How to search a text by ignoring the case?''' Pass the option -i (ignore case), which will ignore the case while searching. <nowiki># </nowiki>grep -i john /etc/passwd jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash '''How do I search all subdirectories for a text matching a specific pattern?''' Use option -r (recursive) for this purpose. In the example below, it will search for the text "John" by ignoring the case inside all the subdirectories under /home/users. This will display the output in the format of "filename: line that matching the pattern". You can also pass the option -l, which will display only the name of the file that matches the pattern. <nowiki># </nowiki>grep -ri john /home/users /home/users/subdir1/letter.txt:John, Thanks for your contribution. /home/users/name_list.txt:John Smith /home/users/name_list.txt:John Doe <nowiki># </nowiki>grep -ril john /root /home/users/subdir1/letter.txt /home/users/name_list.txt
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)