




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















GREP and SED Command in Unix Submitted by arunraj on Tue, 12/07/2010 - 19: In this article let's see the basic usage of GREP and SED commands in Unix platform.
What is Grep?
Grep (Global Regular Expression Processor) is a command used for searching for text, patterns (regex) in a file or a set of files. Let see the usage of Grep command using the following example.
country.txt Afghanistan Albania Algeria United States
Dubai Indonesia Armenia To find the list of countries starting with a use the following command
$ cat country.txt | grep A.*
Afghanistan Albania
Algeria Austria Australia To do the above operation with case insensitive use the following command
$ cat country.txt | grep I A.*
Afghanistan
To search for a particular country use the following command
$ cat country.txt | grep 'Austria'
Austria You can also use the grep command without using the cat command as follows
$ grep Austria country.txt
Austria To find a match in all the files in a directory use the recursive grep command
$ grep -r "king" /home/examples/ To get the line number of a match in the file use the following command
Afghanistan Albania Algeria United States Australia Chile Mongolia China France
Italy India Finland Dubai Indonesia Armenia What is Sed?
Afghanistan Albania Algeria United States Australia Chile Mongolia China France
Italy India Finland Dubai Indonesia Armenia The format to use SED command is
sed - delete the line in the actual file then you need to redirect the output to the actual file as follows
$ cat country.txt | sed -e '/Austria/d' > countryNew.txt To delete the first line in a file use the following command
$ cat country.txt | sed -e 1d Here you need not specify any match because we are going to delete only first line.
Albania Algeria United States Austria Australia Chile Mongolia China
Use the following command to delete blank lines in a file
$ cat country.txt | sed -e '/^$/d' Search and Replace
To find a match and replace it with a given word, we can use the SED command in the following way
The format is as follows
Format:
sed -e s/// filename
Example: $ sed -e 's/Austria/Australia/' country.txt