Loop through file and replace with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through file and replace with sed
# 1  
Old 07-15-2014
Loop through file and replace with sed

Hello all,

I need some help please.

I got file1 with names.

Code:
foo bar
foo bar
foo bar
foo bar
foo bar

and I got file2 with some text
Code:
some text
some text
#KEYWORD

some text
some text
some text

after the #KEYWORD I need to add the following line
Code:
email "foo bar"

in double quotes and basically loop through file1.

Code:
#!/bin/sh

while read line
do
sed '/#KEYWORD/a email = "'${line}'"' file2 > file3
# do stuff with file3 and keep looping
sleep 10
done < file1

cat file3

Code:
some text
some text
#KEYWORD
email = "foo

some text
some text
some text

Why not some email = "foo bar" ?????

Thanks guys

Last edited by Scrutinizer; 07-15-2014 at 06:20 PM.. Reason: Extra CODE tags
# 2  
Old 07-15-2014
That is because $line is used unquoted and thus subject to field splitting by the shell (so only the first field will be part of the sed script in quotes).

Try:
Code:
sed "/#KEYWORD/a email = \"${line}\"" file2
...
done < file1 >file3

---
Note: Only GNU sed supports the append (a) command like this..

Last edited by Scrutinizer; 07-15-2014 at 06:56 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-15-2014
Thanks ever so much
# 4  
Old 07-15-2014
With other quote type and standard sed syntax
Code:
sed '/#KEYWORD/a\
email = "'"${line}"'"' file2 > file3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to replace inside file

How can I change the comma sign (,) to plus sign (+) with the sed command or any regex? I mean to change only the comma that beteen the quotation marks. From this file: A,B,C A,"B,C",D "A,B",C,D A,B,"C,D" To this file: A,B,C A,"B+C",D "A+B",C,D A,B,"C+D" (6 Replies)
Discussion started by: elior
6 Replies

2. Shell Programming and Scripting

Sed, replace variables in file

Hi Everyone, I need some help with sed and I'm totally new to it. I have a template file with variables in it. These variables start with a '$' sign and are exactly one character long (plus the '$' sign). For example: $a, $b, etc. Each variable should be replaced with the contents of a... (9 Replies)
Discussion started by: csarli2006
9 Replies

3. UNIX for Dummies Questions & Answers

Using sed to replace / in text file

Hi, I want to use sed to replace " /// " with "///" in a text file. However I am getting error messages when I use sed 's/ /// /////g' input.txt > output.txt. How do I go about doing this in sed? Input: 219518_s_at 0.000189 ELL3 / SERINC4 Output: 219518_s_at 0.000189 ELL3/SERINC4 (5 Replies)
Discussion started by: evelibertine
5 Replies

4. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

5. Shell Programming and Scripting

Replace file name extension in loop

Hi! I have this shell script that I need to finish. Currently I need to fix one line to make it work. I need to change a file extension. See this code, is pretty simple. #!/bin/sh # Extensions OLD_EXT=.flv NEW_EXT=.mp4 COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l) if ; then ... (8 Replies)
Discussion started by: pulsorock
8 Replies

6. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

7. Shell Programming and Scripting

Sed save changes to same file in loop

I have got problems saving sed changes to the same file in a loop. Basically I want the delimited value in every line of the file to be set to blank according to the value stored in var. var can be changed anytime. I do not have sed -i and i've tried to mv the file. Any other ideas? My file... (8 Replies)
Discussion started by: alienated
8 Replies

8. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

Replace the file using sed

I have file with 70 lines. I want to add 'SLV' at the starting of the line if the line has first character as 0 or 1 or 2 or ..9 Nothing else has to be changed in the file. Please anyone advice. (5 Replies)
Discussion started by: senthil_is
5 Replies
Login or Register to Ask a Question