Difference between revisions of "Sed"

From Notes_Wiki
m
m
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
<yambe:breadcrumb>Shell scripting</yambe:breadcrumb>
[[Main_Page|Home]] > [[Shell scripting]] > [[Sed|sed]]
=sed=


<tt>sed</tt> 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 <tt>sed</tt>, which can do search and replace on stream and then <tt>sed</tt> output can be piped to <tt>nc</tt> etc. to be transmitted over network. Hence replace and network transmission can go in parallel without need of any temporary files.
<tt>sed</tt> 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 <tt>sed</tt>, which can do search and replace on stream and then <tt>sed</tt> output can be piped to <tt>nc</tt> etc. to be transmitted over network. Hence replace and network transmission can go in parallel without need of any temporary files.
Line 58: Line 57:
sed ':a;N;$!ba;s/\n/ /g'
sed ':a;N;$!ba;s/\n/ /g'
</pre>
</pre>
Learned from http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n
Note that some also recommend using tr instead of sed as
<pre>
tr '\n' ' ' < input_filename
</pre>
 
Learned from http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n
 
 


<yambe:breadcrumb>Shell scripting</yambe:breadcrumb>
[[Main_Page|Home]] > [[Shell scripting]] > [[Sed|sed]]

Latest revision as of 13:41, 7 April 2022

Home > Shell scripting > 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


Home > Shell scripting > sed