Search My Techie Guy

Monday, October 4, 2010

Text Manipulation with SED

SED in full is STREAM EDITOR - a UNIX utility used to manipulate text files.
It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.

In this post I present to you practical examples of SED. I have tried out these particular “one liners” and they have saved me a lot of time on several occasions.

As a Unix Admin, it’s important to understand SED because it will save you a lot of time when it comes to generating reports from a batch of text files, log analysis and data extraction from huge text files.

This list will keep growing with time as I get to use more of these “SED expressions”, so please feel free to track this post for any updates. Cheers and have FUN.

Extracting Numerical values from an Alphanumeric String
echo 123gvc4f5jy6 | sed 's#[^0-9]##g'

Joining a pair of lines side-by-side
cat my_file.txt | sed '$!N;s/\n/ /'

Search and replace a string marching a certain pattern
sed 's/bad/good/g' file_a.txt
The above command will search for "bad" and replace it with "good"

Insert a string at the begining of each line in a text file; e.g. insert "256"
cat my_file.txt |sed 's/^/256/' 

Append a character at the end of each line in a text file; e.g. append a semicolon (;)

cat my_file.txt |sed 's/$/;/'

To jump to a whole list of SED one liners, click on any of the links below:


http://sed.sourceforge.net/sed1line.txt
http://www.pement.org/sed/sed1line.txt

No comments: