Ad

Wednesday, July 9, 2014

SED unix utility explained

SED is a great unix utility that enables you to manipulate the contents of a file with simple regular expressions. Say for instance if you wanted to remove the first line in a file:

sed -i '1d' /filepath/hello.txt

The above command specified to insert the text into the file i.e. modify the file specified after the command.
The '1d' tells sed to delete the first line from the file.

You can also replace text in files e.g:

sed -i 's/text your looking for/text you want to replace it with/' /filepath/hello.txt

The above statement will find the text: "text your looking for" and replace it with "text you want to replace it with" the 's' in the beginning stands for pattern space. The syntax explained:

s/regex/replacement/

Have fun with it!

No comments: