Saturday, May 17, 2008

Format lines with sed


Input file:

$ cat file.txt
+*MYVAR6255
ASDF
ASWE
+*myvar2323
ASWER
+*MyVaR2312
+*myVAr2555
ASQWE


Required Output:

MYVAR 6255
myvar 2323
MyVaR 2312
myVAr 2555

i.e.
- print the lines beginning with +*, separate [Mm][yY][Vv][Aa][rR] from the digit part in all those lines.

Sed solution:

$ sed '/^+/!d; s_\+\*\([a-zA-Z]*\)\([0-9]*\)_\1 \2_' file.txt


Explanation:

/^+/!d :: Print only those lines beginning with +*
\([a-zA-Z]*\) :: Remembering the string part of the line
\([0-9]*\) :: Remembering the digit part.
Printing them using \1 and \2 with a space in between
_ is used as delimiter instead of / to avoid confusion.


If you need more clarification, feel free to post a comment :-)

No comments:

© Jadu Saikia www.UNIXCL.com