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:


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:


PERL REGULAR EXPRESSIONS: MATCHING TEXT WITH REGULAR EXPRESSIOS, ~M

In Perl, The “m” means to attempt a regular expression match. The requrlar expressions, or search patterns, are put between to forwardslashes, “/../”. And the string that to be searched is linked by” =~”. Truse, if you want to search any digits from a string, you can do the following:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
while ( $varString=~ m/(\d+)/g) {
print "Found digits: $1\n";
}

And the output will be:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22
Found digits: 1732

Here the search pattern is “/(\d+)/”, “m” means match and ‘=~” tells perl to link to the string “$varString”. Lastly, “g” means do a global search on the string (from left to right). The search resulf, is stored in “$1”.

If we changed the code a little bit, without using the while loop and without using global search “g”::

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString=~ m/(\d+)/;
print "Found digits: $1\n";

The outout will be:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22

Perl find the first digits and stop and the resulf still store in $1.

Now, assuming that we forgot to use “~” and have the codes as the following:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString= m/(\d+)/;
print "Found digits: $1\n";

The output are:

Search string "George Washington was born on February 22, 1732" for digits
Use of uninitialized value in pattern match (m//) at ./perl_rex.pl line 8.
Use of uninitialized value in concatenation (.) or string at ./perl_rex.pl line 9.
Found digits:

The reason is when the “~” is omitted, perl is looking for a match of the regex in $_ and stores the search resulf to $varString.
Lets test it using the following codes:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$_="George Washington was born on February 22, 1732";
$varString= m/(\d+)/;
print "Found digits: $1\n";
print "Found digits: $varString\n";

And the output are:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22
Found digits: 22

In fact, “m” is not reuested as long as you have used “~”, but using “~m” make the codes a bit easy to read, in my opions.

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString=~ /(\d+)/;
print "Found digits: $1\n";

References: