Skip to content

Tag: sed

Replacing with SED and regexes

Let’s say you have a html file called file.html and you want to replace “.jpg” to “.png” but only when in a href of anchor elements.

Example of input:

<a href="alice.jpg">alice<a>
<a href="bob.jpg">bob<a>
something.jpg
href.jpg
<a href="example.com">alice.jpg</a>
<img src="href.jpg">

Desired output:

<a href="alice.png">alice<a>
<a href="bob.png">bob<a>
something.jpg
href.jpg
<a href="example.com">alice.jpg</a>
<img src="href.jpg">

Notice that only the first two references to “.jpg” were changed to “.png”, the ondes in the href of the anchor.

You can use sed with regexes to achieve this.

sed -i -E 's/(<a href=".*).jpg(")/\1.png\2/' file.html

Where:

  • -i for editing the files in-place
  • -E to use a script
  • s// substitute
  • (<a href=”.*) group 1, the string ‘<a href=”‘ followed of any character zero or more times
  • .jpg the .jpg we want to replace
  • (“) group 2, only “
  • \1.png\2 substitute with the same group 1 then .png then the group 2.

printing only odd or even lines using sed

$ cat AtoF
A
B
C
D
E
F

Print only odd lines

sed -n 'p;n' AtoF
A
C
E

-n suppress automatic printing of pattern space, p print the current pattern space, n read/append the next line of input into the pattern space.

Alternatively:

$ sed -n 1~2p AtoF
A
C
E

-n suppress automatic printing of pattern space, 1~2 from the 1st line match every line every 2 steps, p print the current pattern space. sed -n 0~p has the same effect . print only the even lines.

Print only even lines

$ sed -n 'n;p' AtoF 
B
D
F

or

$ sed -n 2~2p AtoF
B
D
F

0~2p would also work. I prefer