?

Log in

No account? Create an account

Previous Entry | Next Entry

Add/Change/Insert lines using sed


 Using sed we can  Add/Change/Insert lines in a file, I found it very useful, hope you too !

$ cat namedb.txt
Nina:London
Apen:India
Lokesh:India

#Add a line
a)
$ sed '
/Apen/ a\
Add this line after Apen
' namedb.txt

<Output>
Nina:London
Apen:India
Add this line after Apen
Lokesh:India

b)
$ sed '
2 a\
Add this line after 2nd line
' namedb.txt

<Output>
Nina:London
Apen:India
Add this line after 2nd line
Lokesh:India

#Insert a new line before
c)
$ sed '
/Apen/ i\
Insert this line after Apen
' namedb.txt

<Output>
Nina:London
Insert this line after Apen
Apen:India
Lokesh:India

Similary one can mention the line number (case b above)

#Change a line

d)
$ sed '
/Apen/ c\
Change the line with Apen to this line
' namedb.txt

<output>
Nina:London
Change the line with Apen to this line
Lokesh:India

Similarly one can mention the line number to change (case b above)

 

Profile

jaduks
Jadu Kumar Saikia

Latest Month

January 2008
S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  
Powered by LiveJournal.com
Designed by chasethestars