The Grep Command Syntax: G/re/p
The Grep Command Syntax: G/re/p
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
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
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;
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
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
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
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
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.
Syntax:
Syntax:
sed -n 'M,Np' FILE.txt
Example:
To print 3rd line to 8th line.
Syntax:
Syntax:
sed -n '/PATTERN/p' FILE.txt
Example:
Syntax:
Syntax:
Syntax:
Syntax:
>cat file.txt
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.
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.
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
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the
string in the 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.
You can use any delimiter other than the slash. As an example if you want to change the web url to another url
as
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.
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.
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.
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
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.
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.
If you use -n alone without /p, then the sed does not print anything.
You can run multiple sed commands by piping the output of one sed command as input to another sed
command.
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.
You can restrict the sed command to replace the string on a specific line number. An example is
The above sed command replaces the string only on the third line.
You can specify a range of line numbers to the sed command for replacing a string.
Here the sed command replaces the lines with range from 1 to 3. Another example is
Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in
the file.
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.
15. Duplicating lines
You can make the sed command to print each line of a file two times.
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 (!).
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.
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.
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.
"Change line"
"Change line"
The sed command can be used to convert the lower case letters to upper case letters by using the transform "y"
option.
Here the sed command transforms the alphabets "ul" into their uppercase format "UL"
The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
Syntax:
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
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.
This Line Has All Its First Character Of The Word With Upper Case.
Syntax:
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.
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.
{n,m} The preceding item is matched at least n times, but not more than m times.
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”.
This Line Has All Its First Character Of The Word With Upper Case.
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
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.
$ cat demo_text
You may want to do several navigation in relation to the words, such as:
-A is the option which prints the specified N lines after the match as shown below.
Syntax:
The following example prints the matched line, along with the 3 lines after it.
-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.
-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.
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'
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" *
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”.
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.
10. display the lines which does not matches all the given pattern.
Syntax:
$ cat test-file.txt
When you want to count that how many lines matches the given pattern/string, then use the option -c.
Syntax:
grep -c "pattern" filename
When you want do find out how many lines matches the pattern
When you want do find out how many lines that does not match the pattern
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.
demo_file
demo_file1
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
is line
When you want grep to show the position where it matches the pattern in the file, use the following
options as
Syntax:
$ cat temp-file.txt
12345
12345
2:3
8:3