This document provides a complete overview of the grep and sed commands in 17 pages. It describes the main functions of grep such as searching files for regular expressions and printing lines. It also explains many options for grep like -A, -B, -C, -c, -f, and -i. The document then describes sed as a stream editor for filtering and transforming text. It provides examples of using sed for substituting strings, changing lines based on line numbers or patterns, deleting/adding lines, and transforming text similar to the tr command.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
173 views17 pages
Grep and Sed Commands
This document provides a complete overview of the grep and sed commands in 17 pages. It describes the main functions of grep such as searching files for regular expressions and printing lines. It also explains many options for grep like -A, -B, -C, -c, -f, and -i. The document then describes sed as a stream editor for filtering and transforming text. It provides examples of using sed for substituting strings, changing lines based on line numbers or patterns, deleting/adding lines, and transforming text similar to the tr command.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 1
GREP
Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the words in a file. The power of grep comes with using its options and regular expressions. You can analyze large sets of log files with the help of grep command. Grep stands for Global search for Regular Expressions and Print.
Contents of grep_test file.
-A This option print the lines after the match in short print NUM lines of trailing context after matching lines.
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 2
-B This option print the lines before the match in short print NUM lines of leading context before matching lines.
-C This option print the lines before and after the match in short print NUM lines between contiguous groups of matches
-b This option print the byte offset within the input file before each line of output. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 3
--color Surround the matching string with the marker find in GREP_COLOR environment variable. three option are = never, always or auto.
-c Print the total number of lines in which pattern had been found, when it is used with -v option it print the total number of lines which doesnt contain the search pattern
-f Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 4
-F
-e and -E -e is strictly the flag for indicating the pattern you want to match against. -E controls whether you need to escape certain special characters. More Explanation : grep understands three different versions of regular expression syntax: basic, extended and perl. In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but may not be available on every system. -e stand for basic, -E stands for extened and -P for perl
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 5
--label=LABEL -H This option prints the filename for each match.
-h This option Suppress the prefixing of filenames on output when multiple files are searched.
-I Process a binary file as if it did not contain matching data. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 6
-i Ignore case distinctions in both the PATTERN and the input files
-L Suppress normal output, instead print the name of each input file from which no output would normally have been printed. Also print the directory name if there in no match inside it.
-l Prints the name of the file where proper match will be found, it will not print the pattern, it will just print the file name. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 7
-m Stop reading a file after NUM matching lines. When the -v or --invert-match option is also used, grep stops after NUM non-matching lines
-n This option prints line number and line for particular pattern match.
-o This option prints the pattern not the whole line. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 8
-q Do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
-r , -R, --recursive Read all files under each directory, recursively; this is equivalent to the -d recurse option.
-s This option suppresses error messages about nonexistent or unreadable files. Without using -s option Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 9
With -s option
-v This option inverts the sense of matching, to select non-matching lines.
-w Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non- Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 10
word constituent char acter.
This can be a example for -s option, check both grep command in above screen shots.
-x, --line-regexp Select only those matches that exactly match the whole line.
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 11
SED COMMAND
Def :: sed command is used as stream editor for filtering and transforming text Content of file.txt
Replacing or substituting string sed 's/unix/linux/' file.txt
Replacing the nth occurrence of a pattern in a line sed 's/unix/linux/2' file.txt
Replacing all the occurrence of the pattern in a line. sed 's/unix/linux/g' file.txt
Replacing from nth occurrence to all occurrences in a line. Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 12
sed 's/unix/linux/3g' file.txt
Changing the slash (/) delimiter Content of sed_test.dat file.
sed 's/https:\/\//www./' sed_test.dat
In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example. sed 's_https://_www._' sed_test.dat
sed 's|https://|www.|' sed_test.dat
Using & as the matched string There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string. Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 13
sed 's/unix/{&}/' file.txt
sed 's/unix/{&&}/' file.txt
Using \1,\2 and so on to \9 Duplicating the replaced line with /p flag sed 's/unix/linux/p' file.txt
Printing only the replaced lines sed -n 's/unix/linux/p' file.txt
If you use -n alone without /p, then the sed does not print anything. Running multiple sed commands sed 's/unix/linux/' file.txt| sed 's/os/system/'
Sed provides -e option to run multiple sed commands in a single sed command Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 14
sed -e 's/unix/linux/' -e 's/os/system/' file.txt
Replacing string on a specific line number sed '3 s/unix/linux/' file.txt
Replacing string on a range of lines sed '1,3 s/unix/linux/' file.txt sed '2,$ s/unix/linux/' file.txt
Replace on a lines which matches a pattern sed '/linux/ s/unix/centos/' file.txt
Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos". How to delete lines from a file. sed '2 d' file.txt sed '5,$ d' file.txt
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 15
How to Duplicate lines sed 'p' file.txt
How to use sed as grep command grep 'unix' file.txt sed -n '/unix/ p' file.txt
You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!). grep -v 'unix' file.txt sed -n '/unix/ !p' file.txt Add a line after a match. sed '/unix/ a "Add a new line"' file.txt
How to change a line after match sed '/unix/ c "Change line"' file.txt Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 16
Transform like tr command sed 'y/ul/UL/' file.txt
Display specific lines (based on line number) of a file using sed command View only the specific lines mentioned by line numbers. sed -n -e Xp -e Yp FILENAME
sed : sed command, which will print all the lines by default. -n : Suppresses output. -e CMD : Command to be executed Xp: Print line number X Yp: Print line number Y FILENAME : name of the file to be processed. How to view the content of file from starting line number to ending line number sed -n 101,110p File_Name
Complete Overview on Grep and Sed command
Created By - Ashutosh Priyadarshi
Page 17
How to add line at first and last in a file sed '1 i first_line' FILE_NAME