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
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
Afghanistan
Albania
Algeria
Austria
Australia
To do the above operation with case insensitive use the following command
Afghanistan
Albania
Algeria
Austria
Australia
Armenia
To find the number of lines in the file country.txt use the following command
17
To search for a particular country use the following command
Austria
You can also use the grep command without using the cat command as follows
Austria
To find a match in all the files in a directory use the recursive grep command
To get the line number of a match in the file use the following command
5:Austria
We can also use ‘-v’ option to find the matches in only those lines which do not contain the given word (match).
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
Afghanistan
Albania
Algeria
United States
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia
The format to use SED command is
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
To delete the first line in a file use the following command
Here you need not specify any match because we are going to delete only first line.
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
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
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
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.
$ 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.
<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.