Insights for productivity and tech related info

Using SED - A Stream EDitor for unix systems

2019-09-27 Code.Reportunix

In this article, I will show a fast and simple way to improve your productivity using a tool that helps with text processing, which can be very useful when you need to manipulate part of the document, to find something or when substitution of words or patterns is needed.

sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language and is available today for most operating systems. sed was one of the earliest tools to support regular expressions, and remains in use for text processing, most notably with the substitution command. Popular alternative tools for plaintext string manipulation and “stream editing” include AWK and Perl.

Using sed for substitution or replacing words.

You can replace some words on a text file, or on a stream by simple using the code bellow on a terminal window:

sed 's/code/report/'

This will change what the stream sent to the sed command, replacing the word code to report. Another way to see that in a terminal window is to stream some text to the sed command:

echo 'my code is done' | sed 's/code/report/'

Using files and a Global replacement.

Let’s say that what you need is to replace every word code on a file.txt to report, with sed, you can simply type the code bellow and it will change every word code to report, of the file.txt, and that will be saved on a newfile.txt. The g on the sed command tells that the replace is Global, thus changing every word found, and not only the first one.

sed 's/code/report/g' file.txt > newfile.txt

Regular Expressions (regex) with sed

They say the real power in sed comes on its support of patterns replacement using regex. In the example bellow it will replace every URL link in a text to a corresponding HTML tag with the link.

sed 's_\<\(ht\|f\)tp://[^ ]*_<a href="&">&</a>_' file.txt

Delete lines with sed

With sed, you can delete lines of a file or stream using the (d) flag.

sed '2d' file.txt

Deleting a range of lines in a file. From line 2 to line 20:

sed '2,20d' file.txt