Enclosing a string with sed

  |   Source

One of the most common problems might be "enclosing" a bunch of lines with some other text. Let's do an example: we want to create some <img /> tags from files in a directory, so to easily create an "image gallery" to be put online.

$ ls

bar.png buz.png foo.png

$

How could we have a list of the files, with <img />? "Easy": use some sed magic!

$ ls > files

$ cat files

bar.png

buz.png

foo.png

`$ sed -i 's/^(.*)$/</img>/g' files

$ cat files

<img src="bar.png">bar.png</img>

<img src="buz.png">buz.png</img>

<img src="foo.png">foo.png</img>

Easy, uh? :)

Comments powered by Disqus
Contents © 2013 David Paleino - Powered by Nikola