​​

sed continues..

Text Editing: Insert, Append and change


Let us take a sample file "player.txt" to perform text editing:

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






Inserting Line:


Inserting line takes place just before the given line number:


$sed '1i\
> Football Team' player.txt
Football Teamid    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues


Here there is no newline, so we are inserting new line with this command:


$sed '1i\
> Football Team\
> ' player.txt
Football Team
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues


similarly, to create a blank line just before the given line number:


$sed '1i\
>  Football Team\
> \
> ' player.txt
Football Team

id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues


Inserting some text before each line in the file:


$sed 'i\
> ------------------------------\
> ' player.txt
------------------------------
id    name    team
------------------------------
1     Amber   Blues
------------------------------
2     Boby    Reds
------------------------------
3     Cris    Blues
------------------------------
4     Rich    Blues
$


Finding a pattern match and inserting text:


$sed '/Amber/i\
> (Amber is the captain here)\
> ' player.txt
id    name    team
(Amber is the captain here)
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues








Append Line:


It is very similar as inserting text, only the difference is appending text takes place just after the given line number:


$sed '3a\
> Reds team player is out of team today\
> ' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
Reds team player is out of team today
3     Cris    Blues
4     Rich    Blues


Append text after finding the matched pattern line:


$sed '/Rich/a\
> 5    Monty   Blues\
> ' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues
5    Monty   Blues



Change Line:


It will the change the text at the same line which is given in command.


$sed '3c\
> 2    Arnold   Green\
> ' player.txt
id    name    team
1     Amber   Blues
2    Arnold   Green
3     Cris    Blues
4     Rich    Blues


Change the line text after matching the pattern:


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







All these text editing commands (Insert, Append, Change) work only for display purpose on the standard output screen, there is no change in the original file "player.txt". So just follow the below method to make the changes permanent. 


Original "player.txt" file contents now:


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


Just we are taking the last change command for this as an example.

$sed '/Rich/c\
> 4    Gill   Reds\
> ' player.txt >> $$;mv $$ player.txt
$
$cat player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4    Gill   Reds


Now last line in the file is changed permanently.


   

Deleting Lines from a file:


Sample file:


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


Delete 2nd line:


$sed '2d' player.txt
id    name    team
2     Boby    Reds
3     Cris    Blues
4    Gill   Reds


Delete line number 3 to last line in file:


$sed '3,$d' player.txt
id    name    team
1     Amber   Blues


Finding the patterns and deleting the pattern lines:


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


Deleting all the lines between the first occurrence of the given two patterns: 


$sed '/Amber/,/Cris/d' player.txt
id    name    team
4    Gill   Reds


Deleting multiple pattern lines in a single command:


$sed -e '/Boby/d' -e '/Gill/d' player.txt
id    name    team
1     Amber   Blues
3     Cris    Blues


Continue with sed

NEXT ->


Cracking IT Interview