From Notes Wiki
Jump to: navigation, search

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


In-place replacement

Although sed in mostly used for stream editing, it is capable of editing files in place too. To replace particular pattern with replacement everywhere in a given set of files one can use:

sed -i 's/pattern/replacement/g' *


Replacing new-lines with spaces

New lines can be replaced with spaces using:

sed ':a;N;$!ba;s/\n/ /g'

Note that some also recommend using tr instead of sed as

tr '\n' ' ' < input_filename

Learned from http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n