I have been using the grep command in UNIX virtually everyday in my work but yet sometimes some of the file manipulation tasks annoy me, especially the pattern matching tasks for which I always needed a quick solution, so I decided to do a writeup on grep command and its practical applications, hope this may make the life of UNIX users a little easier.
The grep series of commands.... For a customary introduction, the grep command is used to find/print lines in a file that matches a pattern, it comes in different flavors, there is an enhanced version of the grep command, namely the egrep which does some advanced regular expression pattern matching on text files and the fgrep command which is used to search text in a file without any regular expession interpretation of the search pattern.
Some common grep usage options....
grep pattern filenames - Displays lines in files which matches the pattern.
grep -i pattern filenames - Displays lines in files which matches the pattern, ignores case while matching the pattern.
grep -c pattern filenames - Prints the count of the number of lines which matches the pattern in files.
grep -l pattern filenames - Lists the names of files which matches the pattern along with the lines which matched the pattern.
Similarly egrep command is used with different enhanced pattern matching options used with regular expressions like
egrep (x|y) filename - Displays lines which match the pattern x or y.
egrep (x|y)+ filename - Displays lines which matches atleast one or more text x or y.
fgrep is mainly used to avoid using search patterns as regular expressions or metacharacters
fgrep '*' filename - matches the occurence of pattern * in filenames, note here * doesn't mean a regular expression or a metacharacter.
Applications of grep command:
grep command can be used as a standalone utility to match a pattern in a file or piped to other utility commands like sed or awk to manilupate and extract text fom files, some of the common applications of grep from simple to little tricky ones are shown below.
The text file used to illustrate the below grep commands is shown below.
There are several ways to save or display a text file with line numbers, some of them are
2. Deleting or Removing all lines in a file which matches a pattern
The grep -v command can be used display lines which DON'T match a pattern or in other words remove lines which matches a pattern in a file.
For example in the above text file, to remove or delete lines which matches the pattern "line 1", the command would be
This is an example which uses the grep command piped to other UNIX commands like sed. To delete only the first line which matches a pattern in a file, use the following grep - sed pipe.
The below command deletes the first line which matches the pattern "line" in the above text file.
4. Deleting the last line which matches a pattern in a file
To delete the last line which matches a pattern in a file, use the grep - sed command pipe as shown below.
The below command deletes the last line which matches the pattern "line" in the above text file.
Likewise there are countless applications of grep command piped with other utility commands like awk, sed, etc to match patterns in a file and I will add more as and when I find something interesting.
The grep series of commands.... For a customary introduction, the grep command is used to find/print lines in a file that matches a pattern, it comes in different flavors, there is an enhanced version of the grep command, namely the egrep which does some advanced regular expression pattern matching on text files and the fgrep command which is used to search text in a file without any regular expession interpretation of the search pattern.
Some common grep usage options....
grep pattern filenames - Displays lines in files which matches the pattern.
grep -i pattern filenames - Displays lines in files which matches the pattern, ignores case while matching the pattern.
grep -c pattern filenames - Prints the count of the number of lines which matches the pattern in files.
grep -l pattern filenames - Lists the names of files which matches the pattern along with the lines which matched the pattern.
Similarly egrep command is used with different enhanced pattern matching options used with regular expressions like
egrep (x|y) filename - Displays lines which match the pattern x or y.
egrep (x|y)+ filename - Displays lines which matches atleast one or more text x or y.
fgrep is mainly used to avoid using search patterns as regular expressions or metacharacters
fgrep '*' filename - matches the occurence of pattern * in filenames, note here * doesn't mean a regular expression or a metacharacter.
Applications of grep command:
grep command can be used as a standalone utility to match a pattern in a file or piped to other utility commands like sed or awk to manilupate and extract text fom files, some of the common applications of grep from simple to little tricky ones are shown below.
The text file used to illustrate the below grep commands is shown below.
bash-3.00# cat textfile.txt1. Displaying or Saving a file with line numbers
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
bash-3.00#
There are several ways to save or display a text file with line numbers, some of them are
bash-3.00# grep -n '^' textfile.txtThe above command matches the first line of the input file ('^' matches a pattern at the beginning of a line), or it matches all the lines in a file, one can also use grep -n '$' textfile.txt to produce the same output (the regular expression '$' means "ends with").
1:This is line 1
2:This is line 2
3:This is line 3
4:This is line 4
5:This is line 5
bash-3.00# grep -n '$' textfile.txtAlso one can use the Regular expression ".*" to match and print all lines in the file with line numbers.
1:This is line 1
2:This is line 2
3:This is line 3
4:This is line 4
5:This is line 5
bash-3.00# grep -n '.*' textfile.txtThe RE . (dot) is used to match a single character and the pattern * is used to match zero or more occurences, thus printing all line numbers when used with grep -n.
1:This is line 1
2:This is line 2
3:This is line 3
4:This is line 4
5:This is line 5
2. Deleting or Removing all lines in a file which matches a pattern
The grep -v command can be used display lines which DON'T match a pattern or in other words remove lines which matches a pattern in a file.
For example in the above text file, to remove or delete lines which matches the pattern "line 1", the command would be
bash-3.00# grep -v "line 1" textfile.txtTo delete lines which matches the pattern "line 1" or "line 3", use the egrep command
This is line 2
This is line 3
This is line 4
This is line 5
bash-3.00# egrep -v '(line 1|line 3)' textfile.txt3. Deleting the first line which matches a pattern in a file
This is line 2
This is line 4
This is line 5
This is an example which uses the grep command piped to other UNIX commands like sed. To delete only the first line which matches a pattern in a file, use the following grep - sed pipe.
The below command deletes the first line which matches the pattern "line" in the above text file.
bash-3.00# grep "line" textfile.txt | sed 1dThe sed 1d command deletes the first line in the above grep output.
This is line 2
This is line 3
This is line 4
This is line 5
4. Deleting the last line which matches a pattern in a file
To delete the last line which matches a pattern in a file, use the grep - sed command pipe as shown below.
The below command deletes the last line which matches the pattern "line" in the above text file.
bash-3.00# grep "line" textfile.txt | sed '$d'The sed $d command deletes the last line in the above grep output.
This is line 1
This is line 2
This is line 3
This is line 4
Likewise there are countless applications of grep command piped with other utility commands like awk, sed, etc to match patterns in a file and I will add more as and when I find something interesting.




No comments:
Post a Comment