GREP and SED Command in Unix

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
Austria
Australia
Chile
Mongolia
China
France
Italy
India
Finland
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
Albania
Algeria
Austria
Australia
Armenia

To find the number of lines in the file country.txt use the following command

$ cat country.txt | grep –c ‘.*

17

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

$ grep  –n  ‘Austria’ country.txt

5:Austria

We can also use ‘-v’ option to find the matches in only those lines which do not contain the given word (match).

$ cat country.txt | grep -v  'Austria'

Afghanistan
Albania
Algeria
United States
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia

What is Sed?
SED (Stream Editor) is a powerful tool or command used for efficient text processing in Unix. Lets see the usage of SED command with the following examples.

To delete the line containing the given match use the following SED command

$ cat country.txt | sed -e '/Austria/d'

Afghanistan
Albania
Algeria
United States
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia

The format to use SED command is

sed  -<option>/<match expression>/<command to be executed>

The SED command gets the stream of input from stdin and then it fills the same in its pattern space.
Then the command after the slash ‘/Austria/d’ (i.e) the ‘d’ which means delete the line containing the given match ‘Austria’. Once the command is executed the content of the pattern space is sent to the stdout.

Note that at the line in the actual file will not be deleted. You will be seeing the output only from the SED commands internal pattern space. To 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
France
Italy
India
Finland
Dubai
Indonesia
Armenia

You can also specify the line number preceding the ‘d’ to delete that particular line.

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/<find expression>/<replace expression>/’ filename

Example:
$  sed  -e  's/Austria/Australia/' country.txt

Output:
Afghanistan
Albania
Algeria
United States
Australia
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia

To use the match as a part of replace string, we can use the following command

$  sed -n -e  's/United States/& of America/p' country.txt

United States of America

As you see the above output, ‘&’ has been used to represent the matched string, which is used as a part of replace string. So when the match ‘United States’ is found it is replace with ‘ United States of America’. ‘-n’ option is used so that the entire output in the file is not printed. ‘p’ command used at the end of the command is used to print only the replaced match.

Convert text files to html files

Here let's do a simple conversion of text to html format. First create a file containing SED commands.

$ cat country.txt | sed -e '1i <html> <body>\
$ a </body> </html> '
> countryHtml.html

The command adds the HTML tags to the first line as ‘i’ is the insert command. Then ‘$ a’ moves to the end of the file content and adds the ending tags to the file content.
So the resulting file content will be as follows.

vi countryHtml.html

<html> <body>
Afghanistan
Albania
Algeria
United States
Austria
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia

</body> </html>

In this way we can use SED command for text processing.

Technology: 

Search