Tuesday, 12 July 2016

Quick and dirty sed command line reference

Below are some commonly used functions performed with sed:

Deleting / stripping the first line of a file:

sed -e '1d' /etc/services

or we can strip the last line of the file with:

sed -e '$d' /etc/services

Deleting a range of lines:

sed -e '1,50d' /etc/services

We can also use regex's with sed - for example a grep style search:

sed -n -e '/^host/p' /etc/services

(The -n option instructs sed not to print out everything else - other than the matches.)

We can also replace patterns in a file using a regex (replaces any line beginning with 'host' with 'nonhost' :

sed -e 's/^host*/nonhost/' /etc/services

If you wanted to pipe the matches out to stdout we could issue something like:

sed -n -e 's/^host*/nonhost/p' /etc/services

0 comments:

Post a comment