Inserting text with SED

Hi guys,

I need to insert @test.com after each entry in my .txt file.

1
2
3
4

1@test.com
2@test.com
3@test.com
4@test.com

Tried to use

cat test.txt |sed 's/$/@test.com/'

but it does this instead:

1
@test.com
2
@test.com
3
@test.com
4
@test.com

Am i doing something wrong?

mv test.txt test.tmp 
sed 's/.*/&@test.com/' test.tmp >test.txt
rm test.tmp

Still same problem.

doing a

vi test.txt

revealed ^M after each entry, and that explains it. I removed the ^M by doing a

:%s/^M//g

Thanks for the input :slight_smile:

@spirm8, your sed statement is correct and it should not be doing that. There probably is something wrong with the input file. What platform are you on, how did you create the input file.
Also, what is the output of

od -c < file.txt

and:

printf "1\n2\n3\n4\n" | sed 's/$/@test.com/'
1 Like
vi test.txt

:%s/.*/&@test.com/

when in vi in command mode, use

:set list

to enable the display of invisible caracter

:set nolist

to disable the display of invisible caracter

1 Like

Thanks, problem is solved - I removed the ^M in vi using:

:%s/^M//g

and now sed is printing the desired :slight_smile:

Thanks for answers

Just for your information, this can also be done using the dos2unix command

1 Like