Sed

From Notes_Wiki
Revision as of 04:00, 5 August 2013 by Saurabh (talk | contribs)

<yambe:breadcrumb>Shell scripting</yambe:breadcrumb>

sed

sed or stream editor is very useful and unique as it can help in search and replace operations on stream. Hence if we want to transmit a document with some pattern replaced over network, then we do not have to first perform search and replace, followed by network transmission. We can pipe the file to sed, which can do search and replace on stream and then sed output can be piped to nc etc. to be transmitted over network. Hence replace and network transmission can go in parallel without need of any temporary files.


Basic search and replace

We can use sed for search/replace within line with backreference using 's/search/replace/'


Deleting blocks of text that span multiple lines

We can use it to delete text which may span multiple lines using regular expressions like

	sed '/lease 10\.1\.[^}]*/,/}/d'

Here we are lookiing for lines that contain 'lease 10.1. followed by anything except } brace' and then from that line go till line that has a closing brace {, and delete the entire set of lines.


Printing matched lines

We can also print the lines if we want to use d to ensure that we are going to delete only what we want to remove and nothing extra. We can print sed matched pattern using:

	sed '/lease 10\.1\.[^}]*/,/}/p'

This will cause each line which matches to be printed twice. We can then use uniq -d to print duplicate lines like:

	sed '/lease 10\.1\.[^}]*/,/}/p' | uniq -d 

to get only lines that would get deleted


Deleting lines by line number

If we need to delete a specific line by line number we can use:

sed -i <line-number>d <file-name>

Example:

sed -i 61d ~/.ssh/known_hosts

<yambe:breadcrumb>Shell scripting</yambe:breadcrumb>