Replace values in script reading line by line using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace values in script reading line by line using sed
# 1  
Old 09-26-2014
Replace values in script reading line by line using sed

Hi all,

Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE.
Code:
for pa in PA_VALUE 
blah blah
do
for pb in PB_VALUE
blah blah
do

I have a text file with two columns of values for PA and PB.
Code:
14.5 16.7
7.8 9.5
5.6 3.6
etc etc

I would like to read this text file line by line and replace the PA_VALUE and PB_VALUE with these values. The first row, first column would replace wherever PA_VALUE occurs in the script and replace the value in the first row, second column wherever PB_VALUE occurs. Then it would repeat line by line in the text document. I have thus been unable to achieve this. I am familiar with sed to replace values, but unsure how to link it to read a document line by line and replace in another script. Thanks.

Last edited by crimsonengineer; 09-26-2014 at 02:10 AM..
# 2  
Old 09-26-2014
Quote:
Originally Posted by crimsonengineer
Hi all,

Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE.
Code:
for PA_VALUE 
blah blah
do
for PB_VALUE
blah blah
do

I have a text file with two columns of values for PA and PB.
Code:
14.5 16.7
7.8 9.5
5.6 3.6
etc etc

I would like to read this text file line by line and replace the PA_VALUE and PB_VALUE with these values. The first row, first column would replace wherever PA_VALUE occurs in the script and replace the value in the first row, second column wherever PB_VALUE occurs. Then it would repeat line by line in the text document. I have thus been unable to achieve this. I am familiar with sed to replace values, but unsure how to link it to read a document line by line and replace in another script. Thanks.
Hello crimsonengineer,

Welcome to forum. Please let us know input and expected output for same.
It will be helpful for us.

Thanks,
R. Singh
# 3  
Old 09-26-2014
So my output would be a script containing the values in the text file:

Quote:
for pa in 14.5
blah blah
do
for pb in 16.7
blah blah
do
I would like my script to repeat for each row in the text file. I think this can be accomplished with sed or awk, but unsure how to piece together the proper commands. Thanks so much.
# 4  
Old 09-26-2014
Quote:
Originally Posted by crimsonengineer
So my output would be a script containing the values in the text file:

I would like my script to repeat for each row in the text file. I think this can be accomplished with sed or awk, but unsure how to piece together the proper commands. Thanks so much.
Hello,

Following may help in same.
Code:
awk '{for(i=1;i<=NF;i++){{if(i%2 != 0){print "for pa in " $i ORS "do" ORS "blah blah" ORS "done\n"} else {print "for pb in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}}' Input_file

Output will be as follows.
Code:
for pa in 14.5
do
blah blah
done
 
for pb in 16.7
do
blah blah
done
 
for pa in 7.8
do
blah blah
done
 
for pb in 9.5
do
blah blah
done
 
for pa in 5.6
do
blah blah
done
 
for pb in 3.6
do
blah blah
done
 
for pa in etc
do
blah blah
done
 
for pb in etc
do
blah blah
done

EDIT: Adding more solutions for same.

Code:
awk '{for(i=1;i<=NF;i++){V=i%2 != 0?(V="pa "):(V="pb "); {print "for " V "in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}'  Input_file
OR
awk '{for(i=1;i<=NF;i++){{if(i%2 != 0){V="pa "} else {V="pb "}; print "for " V "in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-26-2014 at 02:32 AM.. Reason: Added 2 more solutions for same
# 5  
Old 09-26-2014
In shell:
Code:
while read pa pb
do
  cat << EOF
for pa in $pa 
blah blah
do
for pb in $pb
blah blah
do
EOF
done < file



--
Note that
Code:
for pa in 14.5
blah blah
do

Is invalid syntax but that wasn't your point..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 09-26-2014
Thanks for the replies. I think this last bit might work. But if I had other conditions in my script ... could I embed it like this:

Code:
for var1 in 1 2
do
for var2 in 3 4
do

while read pa pb
do
  cat << EOF
for pa in $pa 
do
for pb in $pb
do
EOF
done < file

[sed commands to replace the above values in another file]

done #end var1
done #end var2

so would the embedded while/do loop also iterate through the lines in the "file" along with the previous for/do loops?
# 7  
Old 09-26-2014
You can use the "here document" (cat << EOF etc) to generate text but not to somehow dynamically generate code that is part of the script itself. So as it is it will print the text to stdout..

But it is unclear what you intend to do. Can you elaborate (for example with the sed command that you mention)?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

5. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

6. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

7. Shell Programming and Scripting

how to use sed for reading from a file, line by line?

I have a file, from where I need to extract some data. But, the condition is like following : The script will read line by line,while checking if any line starts with 'MN'. If found true, it looks for if the immediate line starts with 'PQ' or not. If its true then, extract few fields from that... (1 Reply)
Discussion started by: mady135
1 Replies

8. UNIX and Linux Applications

Reading values from the command line

Hi I want to give the user the choice of whether or not they want to include a certain option when they run the script. This is my getops: while getopts " s: d: r f: e h " option do case $option in f ) dsxfile="$OPTARG";; d ) dbname="$OPTARG";; s ) dsn="$OPTARG";; r )... (0 Replies)
Discussion started by: ladyAnne
0 Replies

9. Shell Programming and Scripting

how can i replace / with new line in shell script or sed ?

1. how can i replace ' / ' with new line in shell script or sed ? 2. how can set a conditon untill null in while loop while ( i== null ) do ...... done (3 Replies)
Discussion started by: mail2sant
3 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question