Thursday, September 02, 2010

Linux Sed: REPLACE, AND DELETE LINES FROM A FILE USING SED

REPLACE, AND DELETE LINES FROM A FILE USING SED


To test each of the commands and options, let’s use the following files as an example:

#> cat sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient
 1. Use Sed ‘c\’ to replace a line indicate by the address

Following is the Syntax to replace a line with address:

#sed 'ADDRESS a\
  Line which you want to replace' filename

‘ADDRESS’ is a number with represents the line, i.e.,3, represent the 3rd line.

Here am examples using sed replace with address command:

• Replace 3rd line with ,” San Francisco, Golden City”

  #> sed '3 c\San Francisco, Golden City' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
San Francisco, Golden City
Paris, The City of Light
Hong Kong, Pearl of the Orient

2 Use Sed ‘c\’ to replace a line that match with a pathern

Following is the Syntax to append a line with a match of a pattern:

#sed '/PATTERN/ c\
   Line which you want to replace' filename

Here is an example using sed replace to replace a line of match of pattern to a file.

• There is a typo in line “New Jersey, Garden Stata” with the word “Stata”. Let’s replace it.
  #>sed '/New Jersey, Garden Stata/ c\New Jersey, Garden State' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden State
Paris, The City of Light
Hong Kong, Pearl of the Orient

2.Sed Delete
2.1 Use Sed ‘d’ to delete line(s) indicate by the address

Following is the Syntax to replace a line with address:
#>sed ‘ADDRESS d’ inputfilename

Please note there is no “backslash”(“\”) after d.

There are some examples use sed delete command to delete line(s) from a input file.
• Example 1: delete the 3rd line from the file.

# sed ‘3d’ sed_edit.txt
New York, Big Apple
Washington DC, The Capital
Paris, The City of Light
Hong Kong, Pearl of the Orient

• Example 2: delete the line 2 to line 4 from the file.

# sed '2,4d' sed_edit.txt
New York, Big Apple
Hong Kong, Pearl of the Orient

2.2 Use Sed ‘d’ to delete line(s) indicate by match of pattern

Following is the Syntax to replace a line with address:

#sed ‘/PATTERN/ d’ inputfilename

Here some examples use sed delete command with match of pattern to delete line(s) from a input file.

• Example 1: delete line matches a given pattern.
  Delete the line “New Jersey, Garden Stata” from the file
  #>sed '/New Jersey, Garden Stata/ d' sed_edit.txt
   New York, Big Apple
   Washington DC, The Capital
   Paris, The City of Light
   Hong Kong, Pearl of the Orient

• Example 2: delete all the blank link from a file
  Let’s change the file to look like this:
  #>cat sed_edit.txt
  New York, Big Apple

 Washington DC, The Capital

 New Jersey, Garden Stata

  Paris, The City of Light
  Hong Kong, Pearl of the Orient

Now, use delete to remove all blank lines.
 #>sed '/^$/d' sed_edit.txt
 New York, Big Apple
 Washington DC, The Capital
 New Jersey, Garden Stata
 Paris, The City of Light
  Hong Kong, Pearl of the Orient

• Example 3: delete multiple lines starting from the the line that matches a given pattern
 Delete the lines that match “Paris” and all lines that after this lines

#>sed '/Paris/,$d' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata

• Example 4: delete 1 addition lines from the match pattern
Delete the line that matches “Paris” and one more line after that

#sed '/Paris/,+1d' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata

References:


No comments: