The name, “grep”, derives from the command used to perform a similar operation, using
the Unix/Linux text editor ed:
g/re/p
The grep command syntax
The syntax is as follows:
grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName
How do I use grep command to search a file?
Search /etc/passwd file for boo user, enter:
$ grep boo /etc/passwd
Sample outputs:
foo:x:1000:1000:foo,,,:/home/foo:/bin/ksh
You can force grep to ignore word case i.e match boo, Boo, BOO and all other
combination with the -i option:
$ grep -i "boo" /etc/passwd
Use grep recursively
You can search recursively i.e. read all files under each directory for a string
“192.168.1.5”
$ grep -r "192.168.1.5" /etc/
OR
$ grep -R "192.168.1.5" /etc/
Sample outputs:
/etc/ppp/options:# ms-wins 192.168.1.50
/etc/ppp/options:# ms-wins 192.168.1.51
/etc/NetworkManager/system-connections/Wired connection
1:addresses1=192.168.1.5;24;192.168.1.2;
You will see result for 192.168.1.5 on a separate line preceded by the name of the file
(such as /etc/ppp/options) in which it was found. The inclusion of the file names in the
output data can be suppressed by using the -h option as follows:
$ grep -h -R "192.168.1.5" /etc/
OR
$ grep -hR "192.168.1.5" /etc/
Sample outputs:
# ms-wins 192.168.1.50
# ms-wins 192.168.1.51
addresses1=192.168.1.5;24;192.168.1.2;
Use grep to search words only
When you search for boo, grep will match fooboo, boo123, barfoo35 and more. You can
force the grep command to select only those lines containing matches that form whole
words i.e. match only boo word:
$ grep -w "boo" file
Use grep to search 2 different words
Use the egrep command as follows:
$ egrep -w 'word1|word2' /path/to/file
Count line when words has been matched
The grep can report the number of times that the pattern has been matched for each file
using -c (count) option:
$ grep -c 'word' /path/to/file
Pass the -n option to precede each line of output with the number of the line in the text
file from which it was obtained:
$ grep -n 'root' /etc/passwd
Sample outputs:
1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh
Grep invert match
You can use -v option to print inverts the match; that is, it matches only those lines that
do not contain the given word. For example print all line that do not contain the word
bar:
$ grep -v bar /path/to/file
UNIX / Linux pipes and grep command
grep command often used with shell pipes. In this example, show the name of the hard
disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo
Printing Lines from a File using sed
Before we start, just remember two points:
1. sed "p" command lets us print specific lines based on the line number or regex provided.
2. sed with option -n will suppress automatic printing of pattern buffer/space. So, we would want to
use this option. (Explained in later section)
For our better understanding, let us have a file sedtest.txt with contents as follows:
$ cat sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
sed -e '1,9d;100q'
That means delete lines 1-9, quit after line 100, and print the rest. For 200 lines it's not
going to matter, but for 200,000 lines the first version will still look at every line even when
it's never going to print them. I prefer the first version in general for being explicit, but with a
long file this will be much faster — you know your data best.
A. sed - Print Lines with Line Number
1. Print 'N'th line
This will print 'N'th line in the FILE.txt.
Syntax:
sed -n 'Np' FILE.txt
Example:
To print 1st line,
$ sed -n '1p' sedtest.txt
This is line #1
While, to print last line,
$ sed -n '$p' sedtest.txt
This is line #10
Let us see, what if we had not used option -n, what would have been the result.
$ sed '5p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
You can easily see that 5th line is printed twice, one from pattern buffer/space and other is the result
of sed '5p'.
2. Print all Lines starting from 'M'th up to 'N'th
This will print the block of lines starting at line number M and ending at line number N.
Syntax:
sed -n 'M,Np' FILE.txt
Example:
To print 3rd line to 8th line.
$ sed -n '3,8p' sedtest.txt
This is line #3
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
Similarly, in order to print lines starting from 5th up to last line, you would run-
$ sed -n '5,$p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
3. Print Every 'N'th Line Starting from 'M'th Line
This will print Mth line and every Nth line coming after that.
Syntax:
sed -n 'M~Np' FILE.txt
Example:
To print every alternate line staring from 2nd one.
sed -n '2~2p' sedtest.txt
This is line #2
This is line #4
This is line #6
This is line #8
This is line #10
B. sed - Print Lines with Regular Expression/Pattern
1. Print lines containing a Pattern
This will print the line that contains the pattern provided.
Syntax:
sed -n '/PATTERN/p' FILE.txt
Example:
$ sed -n '/4/p' sedtest.txt
This is line #4
2. Print lines excluding a Pattern
This will print all those lines which do not contain the pattern provided.
Syntax:
sed -n '/PATTERN/!p' FILE.txt
Example:
$ sed -n '/4/!p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
3. Print Block of lines starting from pattern matching line
This will start printing the lines from the one where pattern matches, till 'N'th line.
Syntax:
sed -n '/PATTERN/,Np' FILE.txt
Example:
$ sed -n '/5/,8p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
Of course, in order to print all the lines starting from pattern matching line till the end, you would
use '/PATTERN/,$p' as follows -
$ sed -n '/5/,$p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
This is line #10
4. Print Block of lines ending at pattern matching line
This will start printing the lines starting from the 'N'th line, till the one where pattern matches.
Syntax:
sed -n 'N,/PATTERN/p' FILE.txt
Example:
$ sed -n '4,/9/p' sedtest.txt
This is line #4
This is line #5
This is line #6
This is line #7
This is line #8
This is line #9
So, here, if you wish to start from first line and end at a line matching a pattern, you would use -
$ sed -n '1,/6/p' sedtest.txt
This is line #1
This is line #2
This is line #3
This is line #4
This is line #5
This is line #6
5. Print Block of lines between two Pattern matches
This will start printing lines from 1st pattern match till 2nd pattern match.
Syntax:
sed -n '/PATTERN1/,/PATTERN2/p' FILE.txt
Example:
$ sed -n '/5/,/8/p' sedtest.txt
This is line #5
This is line #6
This is line #7
This is line #8
Sed Command in Unix and Linux Examples
Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to
the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use
sed to replace text. You can do many things apart from replacing text with sed. Here I will describe the
features of sed with examples.
Consider the below text file as an input.
>cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
Sed Command Examples
1. Replacing or substituting string
Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word
"unix" with "linux" in the file.
>sed 's/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the
"linux" is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the
second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command
replaces the second occurrence of the word "unix" with "linux" in a line.
>sed 's/unix/linux/2' file.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
3. Replacing all the occurrence of the pattern in a line.
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the
string in the line.
>sed 's/unix/linux/g' file.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
4. Replacing from nth occurrence to all occurrences in a line.
Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line.
The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.
>sed 's/unix/linux/3g' file.txt
unix is great os. unix is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.
5. Changing the slash (/) delimiter
You can use any delimiter other than the slash. As an example if you want to change the web url to another url
as
>sed 's/http:\/\//www/' file.txt
In this case the url consists the delimiter character which we used. In that case you have to escape the slash
with backslash character, otherwise the substitution won't work.
Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter
to another character as shown in the below example.
>sed 's_http://_www_' file.txt
>sed 's|http://|www|' file.txt
6. Using & as the matched string
There might be cases where you want to search for the pattern and replace that pattern by adding some extra
characters to it. In such cases & comes in handy. The & represents the matched string.
>sed 's/unix/{&}/' file.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.
>sed 's/unix/{&&}/' file.txt
{unixunix} is great os. unix is opensource. unix is free os.
learn operating system.
{unixunix}linux which one you choose.
7. Using \1,\2 and so on to \9
The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on.
The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you
want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as
below.
>sed 's/\(unix\)/\1\1/' file.txt
unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.
The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the
words "unixlinux" as "linuxunix", the sed command is
>sed 's/\(unix\)\(linux\)/\2\1/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.
Another example is switching the first three characters in a line
>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt
inux is great os. unix is opensource. unix is free os.
aelrn operating system.
inuxlinux which one you choose.
8. Duplicating the replaced line with /p flag
The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is
not replaced, then the /p prints that line only once.
>sed 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.
9. Printing only the replaced lines
Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses
the duplicate rows generated by the /p flag and prints the replaced lines only one time.
>sed -n 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.
If you use -n alone without /p, then the sed does not print anything.
10. Running multiple sed commands.
You can run multiple sed commands by piping the output of one sed command as input to another sed
command.
>sed 's/unix/linux/' file.txt| sed 's/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
Sed provides -e option to run multiple sed commands in a single sed command. The above output can be
achieved in a single sed command as shown below.
>sed -e 's/unix/linux/' -e 's/os/system/' file.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
11. Replacing string on a specific line number.
You can restrict the sed command to replace the string on a specific line number. An example is
>sed '3 s/unix/linux/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
The above sed command replaces the string only on the third line.
12. Replacing string on a range of lines.
You can specify a range of line numbers to the sed command for replacing a string.
>sed '1,3 s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here the sed command replaces the lines with range from 1 to 3. Another example is
>sed '2,$ s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in
the file.
13. Replace on a lines which matches a pattern.
You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed
command looks for the string to be replaced and if it finds, then the sed command replaces the string.
>sed '/linux/ s/unix/centos/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.
Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix"
with "centos".
14. Deleting lines.
You can delete the lines a file by specifying the line number or a range or numbers.
>sed '2 d' file.txt
>sed '5,$ d' file.txt
15. Duplicating lines
You can make the sed command to print each line of a file two times.
>sed 'p' file.txt
16. Sed as grep command
You can make sed command to work as similar to grep command.
>grep 'unix' file.txt
>sed -n '/unix/ p' file.txt
Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the
pattern.
You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).
>grep -v 'unix' file.txt
>sed -n '/unix/ !p' file.txt
The ! here inverts the pattern match.
17. Add a line after a match.
The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a
new line after a match is found.
>sed '/unix/ a "Add a new line"' file.txt
unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"
18. Add a line before a match
The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a
new line before a match is found.
>sed '/unix/ i "Add a new line"' file.txt
"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.
19. Change a line
The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to
change the line.
>sed '/unix/ c "Change line"' file.txt
"Change line"
learn operating system.
"Change line"
20. Transform like tr command
The sed command can be used to convert the lower case letters to upper case letters by using the transform "y"
option.
>sed 'y/ul/UL/' file.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.
Here the sed command transforms the alphabets "ul" into their uppercase format "UL"
1. Search for the given string in a single file
The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
2. Checking for the given string in multiple files.
Syntax:
grep "string" FILE_PATTERN
This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1.
The grep output will also include the file name in front of the line that matched the specific pattern as
shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files
as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it
matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4. Match regular expression in files
Syntax:
grep "REGEX" filename
This is a very powerful feature, if you can use use regular expression effectively. In the following
example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-
between. i.e To search “lines[anything in-between]empty” in the demo_file.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition
operators:
? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.
5. Checking for full words, not for sub-strings using grep -w
If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a
normal search will show out all the lines.
The following example is the regular grep where it is searching for “is”. When you search for “is”,
without any OPTION IT will show out “is”, “his”, “this” and everything which has the substring “is”.
$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The following example is the WORD grep where it is searching only for the word “is”. Please note that
this output does not contain the line “This Line Has All Its First Character Of The Word With Upper
Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not
for “this”.
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6. Displaying lines before/after/around the match using grep -A, -B and -C
When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel
handy if grep can show you not only the matching lines but also the lines after/before/around the
match.
Please create the following demo_text file for this example.
$ cat demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
* e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.1 Display N lines after match
-A is the option which prints the specified N lines after the match as shown below.
Syntax:
grep -A <N> "string" FILENAME
The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 Display N lines before match
-B is the option which prints the specified N lines before the match.
Syntax:
grep -B <N> "string" FILENAME
When you had option to show the N lines after match, you have the -B option for the opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
6.3 Display N lines around match
-C is the option which prints the specified N lines before the match. In some occasion you might want
the match to be appeared with the lines from both the side. This options shows N lines in both the
side(before & after) of match.
$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
7. Highlighting the search using GREP_OPTIONS
As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight
which part matches the line, then you need to follow the following way.
When you do the following export you will get the highlighting of the matched searches. In the following
example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown
below.
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
8. Searching in all files recursively using grep -r
When you want to search in all the files under the current directory and its sub directory. -r option is the
one which you need to use. The following example will look for the string “ramesh” in all the files in the
current directory and all it’s subdirectory.
$ grep -r "ramesh" *
9. Invert match using grep -v
You had different options to show the lines matched, to show the lines before match, and to show the
lines after match, and to highlight match. So definitely You’d also want the option -v to do invert match.
When you want to display the lines which does not matches the given string/pattern, use the option -v
as shown below. This example will display all the lines that did not match the word “go”.
$ grep -v "go" demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
10. display the lines which does not matches all the given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
$ grep -v -e "a" -e "b" -e "c" test-file.txt
11. Counting the number of matches using grep -c
When you want to count that how many lines matches the given pattern/string, then use the option -c.
Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
When you want do find out how many lines matches the pattern
$ grep -c this demo_file
When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
12. Display only the file names which matches the given pattern using grep -l
If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-
case L) option.
When you give multiple files to the grep as input, it displays the names of file which contains the text
that matches the pattern, will be very handy when you try to find some notes in your whole directory
structure.
$ grep -l this demo_*
demo_file
demo_file1
13. Show only the matched string
By default grep will show the line which matches the given pattern/string, but if you want the grep to
show out only the matched string of the pattern then use the -o option.
It might not be that much useful when you give the string straight forward. But it becomes very useful
when you give a regex pattern and trying to see what it matches as
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14. Show the position of match in the line
When you want grep to show the position where it matches the pattern in the file, use the following
options as
Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345
$ grep -o -b "3" temp-file.txt
2:3
8:3