SED Edition

RegEx Review:

Regular Expressions are key for mastering AWK & SED

****MetaCharacters****

^ – Matches the character(s) at the beginning of the line

ex:
a. # sed -ne ‘/^D/p’ animals.txt
Dam
Doe
Dog
Dso
Dzo

$ – Matches the chracter(s) at the end of the line

ex:
a. # sed -ne ‘/g$/p’ animals.txt
Dog

options used with sed
-n – supress the non matching line and prints only the line that matches
-e – for expessions with sed
p – prints to stdout
I – case insensitive search and matches Dog,DOG,doG….etc,.
Match line which contains only ‘dog’
a. # sed -ne ‘/^Dog$/p’ animals.txt
Dog

Different ways using SED:

a. sed -ne ‘/dog/p’ – reads from STDIN , Press enter after each line . Terminate with CNTRL-D , Prints the matching line after pressing enter.

# sed -ne ‘/dog/p’
dog
dog
Dog
cat
b. # cat animals.txt | sed -ne ‘/dog/Ip’
Dog

c. # sed -ne ‘/^dog/Ip’ animals.txt
Dog

. – Dot matches any character (typically except the new line)
e. # sed -ne ‘/^d..$/Ip’ animals.txt
Dam
Doe
Dog
Dso
Dzo

 

**** REGEX Quantifiers****

* – 0 or more matches of the previous character
+ – 1 or more matches of the previous character
? – 0 or 1 matches of the previous character

This matches the line starts with ‘d’ and follows with any no of characters. ‘d’ alone in the file will not match
a. # sed -ne ‘/^d.\+/Ip’ animals.txt
Dam
Doe
Dog
Dso
Dzo
This matches the line starts with ‘d’ and follows with any no of character , d alone in line will also matches
b. # sed -ne ‘/^D.*/Ip’ animals.txt
Dam
Doe
Dog
Dso
Dzo

Sed prints the whole line of matched Regex , This below example actually matches (D or d with any character 0 time or 1 time)

c.# sed -ne ‘/^Do\?/Ip’ animals.txt
Dam
Doe
Dog
Dso
Dzo
Note : Escape Quantifies in RegExes using backslash to work properly

****Character Classes****

Allows to search for a range of characters
1. [0-9]
2.[a-z][A-Z]
Note: character classes matches 1, and only one character

matches ‘d/D’ at the beginning of the line , any character more than 1 time ,followed by number
a. # sed -ne ‘/^D.\+[0-9]/Ip’ animals1.txt
Doe6
Dog1
Dzo5

b. # sed -ne ‘/^Do[a-z]/Ip’ animals1.txt
Doe6
Dog1

======================================

I used this following files to describe the examples above

——————-

animals.txt : https://drive.google.com/open?id=0B3lpilcXUYo1bU54dy01ZW5JaWc

animals1.txt : https://drive.google.com/open?id=0B3lpilcXUYo1Q3V0bHFLbEJxM0k

——————-

####Introduction to SED####
Usage:-
1.sed [options] ‘instructions’ file or | or STDIN
2.sed -e ‘instruction’ -e ‘instruction’ ……
note: -e is optional if there is only 1 instruction
3.sed -f file_name file_name2 ……. (-f to follow a txt file to process)
Note : Execute SED by indicating instruction on one of the following:
1. commmand line
2. Script file
Note: Sed accepts instructions based on ‘/pattern to match/action’
****Print Specific Lines**** (prints last pritable line ..blank lines are ignored)
a. sed -n ‘1p’ animals.txt (Prints first line of the file)
b. sed -n ‘1p’ animals.txt (Prints last printable line of the file)
c. sed -n ‘2,5p’ animals.txt (Prints 2-5 lines of the file )
d. sed -n ‘1!p’ animals.txt (Prints all lines of the file except 1st)
e. sed -n ‘1,6!p’ animals.txt (Prints all lines of the file except 1-6)
f. sed -ne ‘/Door/,/deer1/p’ animals.txt (prints line from ist match to last match on file)
g. sed -ne ‘/Door/,+2p’ animals.txt (Prints 2 lines aditionally from the first match)
****Deleting Specific Lines**** (use ‘d’ as action key to delete lines in output – doesnt change the orginal file)
a. sed -ne ‘/^$/d’ animals.txt (Deletes empty/blank lines dont output’s to STDOUT)
Note : Drop -n (supress) option to see the output
*Supports all other actions we used above
b. sed -e ‘1~2d’ animals.txt (Deletes all Odd lines)

 

Advertisements