Thursday, April 26, 2012

Using SED to Append or Prepend characters

You can use sed to Append/Prepend characters to a String e.g.

We have a file (test.txt) below and we would like to Prepend each capital letter with a space:
$ cat test.txt
Hello.ThisIsATestFile.

$ sed "s/[A-Z]/ &/g" test.txt
Hello. This Is A Test File.

sed makes use of a special character "&" which implies the pattern found.

Similarly we could Append a sentence after the "Hello." string:
$ sed "s/Hello./&HowAreYou?/g" test.txt 
Hello.HowAreYou?ThisIsATestFile.

No comments:

Post a Comment