sed insert text without newline

Hi,

I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file:

> cat myfile
This is first line.

> sed -i '1i\foo' myfile

> cat myfile
foo
This is first line.

Instead I want myfile to look like this after insert:

fooThis is first line.

Could someone help me? Thanks.

Hi tdw,

Maybe trying with:

echo "This is first line." | sed 's/\(^[A-Z]\)/foo\1/g'
fooThis is first line.

Regards.

1 Like

Another way:

sed -i '1s/^/foo/' myfile
1 Like

Sorry, I was forgetting the part of adding only in first line,

Try with:

echo "This is first line.
This is second line.
This is third line." | sed '1 s/\(^[A-Z]\)/foo\1/'
fooThis is first line.
This is second line.
This is third line.

Regards

Similarly..

echo 'This is first line.'|sed 's/./foo&/'
or 
sed '1s/./foo&/' inputfile > outfile
1 Like

Thank you for all your replies. I got it working.