Upload
satks
View
253
Download
0
Embed Size (px)
Citation preview
8/11/2019 Grep and Sed Commands
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.
8/11/2019 Grep and Sed Commands
2/17
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.
8/11/2019 Grep and Sed Commands
3/17
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.
8/11/2019 Grep and Sed Commands
4/17
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
8/11/2019 Grep and Sed Commands
5/17
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.
8/11/2019 Grep and Sed Commands
6/17
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.
8/11/2019 Grep and Sed Commands
7/17
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.
8/11/2019 Grep and Sed Commands
8/17
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
8/11/2019 Grep and Sed Commands
9/17
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-
8/11/2019 Grep and Sed Commands
10/17
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.
8/11/2019 Grep and Sed Commands
11/17
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.
8/11/2019 Grep and Sed Commands
12/17
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.
8/11/2019 Grep and Sed Commands
13/17
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
8/11/2019 Grep and Sed Commands
14/17
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
8/11/2019 Grep and Sed Commands
15/17
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
8/11/2019 Grep and Sed Commands
16/17
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
8/11/2019 Grep and Sed Commands
17/17
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
sed '$ a last_line' FILE_NAME