Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fuss:sed [2015/01/09 05:34]
127.0.0.1 external edit
fuss:sed [2017/02/22 18:30] (current)
Line 1: Line 1:
 +====== Stripping Comments and Newlines ======
 +
 +<code bash>
 +cat /​etc/​squid/​squid.conf| sed -e '​s/​^#​.*//​g'​ | sed -e ':​r;​N;​s/​\n$//​g;​br'​
 +</​code>​
 +
 +First part:
 +<code bash>
 +sed -e '​s/​^#​.*//​g'​
 +</​code>​
 +strips any line beginning (''​^''​) with ''#''​ and anything that follows after the hash.
 +
 +Next part:
 +<code bash>
 +sed -e ':​r;​N;​s/​\n$//​g;​br'​
 +</​code>​
 +
 +  - create register ''​r'',​
 +  - put newline (''​N''​) in register r,
 +  - substitute all ending newlines (''​\n$''​) with nothing ''/''''/'',​
 +  - repeat with register ''​r''​.
 +
 +====== Insert a Space After Every Character ======
 +
 +Using bash:
 +<code bash>
 +cat test | sed -s '​s/​\([a-zA-Z0-9]\)/​\1 /g'
 +</​code>​
 +
 +Note that ''​sed''​ needs the grouping operators ''​(''​ and ''​)''​ escaped.
 +
 +====== Insert a Line at a Given Position ======
 +
 +Suppose we have a text file containing the following lines:
 +<​code>​
 +number 1
 +number 3
 +</​code>​
 +
 +and that we want to insert ''​number 2''​ as the second line between ''​number 1''​ and ''​number 3'',​ we would issue:
 +
 +<code bash>
 +sed -i '​2inumber 2' file.txt
 +</​code>​
 +
 +where the parts of the fragments represent:
 +  * ''​2''​ the line number,
 +  * ''​i''​ for the insert command, and
 +  * ''​number 2''​ the data that has to be inserted.
 +
 +which will modify the file in-place such that the contents of the file after the insertion becomes:
 +<​code>​
 +number 1
 +number 2
 +number 3
 +</​code>​
 +
 +====== Print all Lines Between Bounds ======
 +
 +To print out all the lines between ''​5''​ and ''​10'',​ issue:
 +<code bash>
 +sed -n '​5,​10p'​ file.txt
 +</​code>​
  

fuss/sed.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.