user1318369 user1318369 - 4 years ago 105
Bash Question

sed command with dollar

I am trying to understand a shell script which uses the command below:

sed '$d;1,2 d;s/^ //' $file1 > $file2

I figured
s/^ //
gets rid of blank lines, but cannot make out what the
$d;1,2 d;
does. Can someone please explain?

Answer Source

This is deleting the last line of the file and the first two lines.

The sed 'd'elete command is expecting one or two arguments. If one, then the delete will happen for that line; if two, then the delete will happen from the line matching the first argument through the line matching the second. The arguments can be regular expressions, or line numbers. sed also recognizes $ for the last line.

So the $d is deleting the last line. and 1,2d is deleting the 1st through 2nd lines.

See man sed for all the gory details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download