Thursday, September 02, 2010

Linux Sed: Print Lines From File Using Sed

Print Lines Using Sed

This is the third note on Text Manipulations with Linux Sed. The first note covers Append and Insert to a file using sed. It also covers the Option Switches. The second note covers Replace and Delete 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 ‘p’ to print line(s) by address

Following is the Syntax to print lines with address:

#sed -n ‘ADDRESS P’ inputfilename

Here some examples use print command with address to print line(s) from a input file.

• Example 1: print the 3r line from a file
#>sed -n '3p' sed_edit.txt
New Jersey, Garden Stata

• Example 2: print a range of lines
Print line 2 to to from the input file

#sed -n '2,4p' sed_edit.txt
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light

• Example 3: Print from line 2 to the end of file.
#>sed -n '2,$p' sed_edit.txt
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient

2 Use Sed ‘p’ to print line(s) by matches of pattern

Following is the Syntax to print lines with address:

#>sed -n ‘/PATTERN/ P’ inputfilename

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

• Example 1: print line matches a patter.
Print out the line that matches that pather “Paris”

$sed -n '/Paris/ p' sed_edit.txt
Paris, The City of Light

• Example 2: print number of lines from the matched pattern
Print line that matches ‘New Jersey’ and the next two lines

#sed -n '/New Jersey/,+2p' sed_edit.txt
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient

• Example 3: print line starting from a given line to the parttern matched line.
Print from 2nd line to the line that matches “Paris”

#>sed -n '2,/Paris/p' sed_edit.txt
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light

• Example 4: print line between two match patterns
Print lines between match patterns “Washington” and “Paris”

#> sed -n '/Washington/,/Paris/p' sed_edit.txt
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light

• Show all the “Lock wait timeout exceeded” errors from the mysql server error log file

#>sed -n -e '/Lock wait timeout exceeded/,+1p' mysql-error.log

“grep” command can do the similar thing, but the +1 (plus 1 line) option, or change to multiple lines, comes handy sometime when we want to inclue messges after the error message. I found it can be helpful to extract messages that are related to an event from any logs (database server logs, syslogs,etc)

• Select all the enteries during a period of time from the Oracle alert log

#sed -n -e '/Thu Jun 4 14:/,/Thu Jun 4 17:/P' alert_ora.log

The above example select the log entries from 2:00pm to 5:00pm on Thursday, July 14.

References:


No comments: