Cracking IT Interview


sed continues..

Substitute Text: 


Replacing text with some other text given in the command. Lets take "player.txt" as a sample file:


$cat worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  parker






Replacing a given pattern with another string, it will substitute only for the first match of pattern:


$sed 's/Parker/Bruce/' worker.txt
11  Bruce            04264554792
32  Peter Bruce     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  parker


Replacing a pattern with another globally for each and every match found, regular expression -character class [Pp] is given for case sensitivity for the first character only:


$sed 's/[Pp]arker/Bruce/g' worker.txt
11  Bruce            04264554792
32  Peter Bruce     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  Bruce


Without using global option "g", the below two commands work in the same way: 


$sed '/[Pp]arker/s//Bruce/' worker.txt
11  Bruce            04264554792
32  Peter Bruce     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  Bruce

$sed '/[Pp]arker/s/[Pp]arker/Bruce/' worker.txt
11  Bruce            04264554792
32  Peter Bruce     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  Bruce


Replacing a pattern only for the first two lines:


$sed '1,2 s/Parker/Bruce/' worker.txt
11  Bruce            04264554792
32  Peter Bruce     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  parker


Replacing multiple patterns with a single command:


$sed -e '1 s/Parker/Bruce/' -e 's/Brian/John/' worker.txt
11  Bruce            04264554792
32  Peter Parker     04653476238
43  John
45  Dorothy           4768256981
68  Linda
87  parker








Using the "&" as replacement string: 


$sed 's/parker/{&}/' worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  {parker}

$sed 's/parker/& & & &/' worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  parker parker parker parker

Removing a pattern for all occurrence:


$sed 's/Linda//g' worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68
87  parker


Substituting a mobile number with replacement string, again using regular expression for the below two patterns:


$sed 's/[0]*[0-9]\{10\}/Contact Mob. &/g' worker.txt
11  Parker            Contact Mob. 04264554792
32  Peter Parker     Contact Mob. 04653476238
43  Brian
45  Dorothy           Contact Mob. 4768256981
68  Linda
87  parker


Replacing two digits numbers at the beginning of each line with given string:


$sed 's/^[0-9]\{2\}/000&/g' worker.txt
00011  Parker            04264554792
00032  Peter Parker     04653476238
00043  Brian
00045  Dorothy           4768256981
00068  Linda
00087  parker



Transform characters:


As we used this in "tr command" for transforming characters in a file. The same can be done with "sed command". Syntax is very similar to substitute, only you have to use "y" as an instruction character:


​$cat worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  parker


At the 6th line, letter "p" is transformed to "P":


$sed '6 y/p/P/' worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  Parker


Find the pattern and then transform the character as given:


$sed '/parker/y/p/P/' worker.txt
11  Parker            04264554792
32  Peter Parker     04653476238
43  Brian
45  Dorothy           4768256981
68  Linda
87  Parker








Counting the lines:


Sample file:


$cat player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4    Gill   Reds


Display the line numbers for each line in file:


$sed '=' player.txt
1
id    name    team
2
1     Amber   Blues
3
2     Boby    Reds
4
3     Cris    Blues
5
4    Gill   Reds


Display the line number for the last line:


$sed '$=' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
5
4    Gill   Reds


Counting the total number of lines in a file:


$sed -n "$=" player.txt
5


Display the line number for the matched pattern line:


$sed '/Cris/=' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
4
3     Cris    Blues
4    Gill   Reds


Display the line numbers from 1st given pattern line to 2nd given pattern line:


$sed '/Amber/,/Cris/{
> =
> }' player.txt
id    name    team
2
1     Amber   Blues
3
2     Boby    Reds
4
3     Cris    Blues
4    Gill   Reds



Questions & Answers:​


Qs: What is the key difference between "sed" and "vi" editor ?

 

  • sed is​ non-interactive mode command where all the text editing operations can be done without opening an editor. "vi" is interactive mode command. we open the editor, edit the text and close the editor to come to the shell.


  • sed command is used only for writing the output result the on standard output screen. There will not be any change with the given file contents (except write instruction). "vi" is having options to save or not to save file contents with an editor itself.


Qs: How to generate duplicate lines in a file?


sed 'p' filename


Qs: What is an alternate command for "grep -v" in sed ?


!p

example: sed -n '/pattern/!p' filename


Qs: How to delete blank lines from a file containing hidden characters like spaces and tabs ?


sed '/^[press one space press one tab]*/d' filename >>$$; mv $$ filename


Qs: How to remove all the comment lines from a shell script file ?


sed 's/#.*//' scriptname

(redirect it to buffer and move again to file for permanent change)


Qs: How to count the total number of lines in a file ?   


sed -n '$=' filename​



 ​​​​NEXT->