Professional Documents
Culture Documents
GREP
GREP
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
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
$ grep �Austria� country.txt
Austria
To find a match in all the files in a directory use the recursive grep 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
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
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.
To find a match and replace it with a given word, we can use the SED command in the
following way
Format:
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
Here let's do a simple conversion of text to html format. First create a file
containing SED commands.
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.