Inserting text with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting text with SED
# 1  
Old 11-12-2010
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
Code:
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?

Last edited by spirm8; 11-12-2010 at 06:06 AM..
# 2  
Old 11-12-2010
Code:
mv test.txt test.tmp 
sed 's/.*/&@test.com/' test.tmp >test.txt
rm test.tmp


Last edited by ctsgnb; 11-12-2010 at 06:55 AM..
# 3  
Old 11-12-2010
Still same problem.

doing a
Code:
vi test.txt

revealed ^M after each entry, and that explains it. I removed the ^M by doing a
Code:
:%s/^M//g


Thanks for the input :-)
# 4  
Old 11-12-2010
@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
Code:
od -c < file.txt

and:
Code:
printf "1\n2\n3\n4\n" | sed 's/$/@test.com/'

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-12-2010
Code:
vi test.txt

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

when in vi in command mode, use
Code:
:set list

to enable the display of invisible caracter
Code:
:set nolist

to disable the display of invisible caracter

Last edited by ctsgnb; 11-12-2010 at 07:41 AM..
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 11-12-2010
Thanks, problem is solved - I removed the ^M in vi using:
Code:
:%s/^M//g

and now sed is printing the desired :-)

Thanks for answers
# 7  
Old 11-12-2010
Just for your information, this can also be done using the dos2unix command
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

2. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

3. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

4. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Inserting text to file, sed and variable not acting right

I want to put text stored in a variable into a file on the 7th line. I'm having trouble with this line: sed '7i\'$text'' $file It works perfectly for a single word, but fails if there are two words because of the space. I've tried several forms of quoting and this is the only one that... (2 Replies)
Discussion started by: fubaya
2 Replies

6. Shell Programming and Scripting

Perl - Inserting text

Hey, I have 10 lines of text ... And I would like to Insert prefix for each line with static text. perl -pi -e 's/()/$1 TEST$./' data.txt Each line will have different static prefix, Code above works perfectly for 1st line ... I'm just not sure how I can run same command for 2nd line 3rd... (4 Replies)
Discussion started by: NDxiak
4 Replies

7. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

8. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

9. UNIX for Dummies Questions & Answers

inserting a text after a certain word in text files

I need insert a text file content in other text file after certain word like insert content of tagfav.txt in all my html files after the <head> tag. Anyone can help me? (2 Replies)
Discussion started by: ItaloAG
2 Replies

10. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies
Login or Register to Ask a Question