Tuesday, March 23, 2010

Prepend string in bash using sed

You can do that using the following sed command

sed 's/^//g'

The key here is the use of ^ Which matches the start of line.

For example below is how you can prepend "foo" to a string "bar"

echo "bar" | sed 's/^/foo-/g'


Anonymous said...

Than you for saving me some time ;)

Anonymous said...

If the data is already in a variable, do this which is much faster:

Esen Sagynov said...

If you want to *append* a string "foo" to a string "-bar" do the following:

echo "foo" | sed 's/$/-bar/g'