Stripping Comments and Newlines

cat /etc/squid/squid.conf| sed -e 's/^#.*//g' | sed -e ':r;N;s/\n$//g;br'

First part:

sed -e 's/^#.*//g'

strips any line beginning (^) with # and anything that follows after the hash.

Next part:

sed -e ':r;N;s/\n$//g;br'
  1. create register r,
  2. put newline (N) in register r,
  3. substitute all ending newlines (\n$) with nothing //,
  4. repeat with register r.

Insert a Space After Every Character

Using bash:

cat test | sed -s 's/\([a-zA-Z0-9]\)/\1 /g'

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:

number 1
number 3

and that we want to insert number 2 as the second line between number 1 and number 3, we would issue:

sed -i '2inumber 2' file.txt

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:

number 1
number 2
number 3

Print all Lines Between Bounds

To print out all the lines between 5 and 10, issue:

sed -n '5,10p' file.txt

fuss/sed.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


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