awk read value from file and paste in other file

Hi,

I'm working on a perl-awk loop combination and what I want to do is read in the first line of values.exp and pass that value to test1.exp; next, read in the second line of that file and pass that value to test2.exp.

Which would mean:

values.exp:

1.2
1.4

test1.exp

1

test2.exp

2

output:

test1.exp: 1 1000 1.2 0.0
test2.exp: 1 1000 1.4 0.0

and i would like to do that x times

here is my script so far:

#!/usr/bin/perl -w

use strict;

for (my $i=0; $i<=x; $i++) {
    print "Processing configuration $i...\n";
    system("awk 'NR==2498{\$1=1;\$2=1000;\$3=14.11;\$4=\"0.0\"}1' test${i}.exp >> test${i}p.exp");
}

With this script i have to change $3 manually, but values are written in values.exp --> so awk should read in this lines for every file.
Could you please help me get this thing done automatically?

Thank you in advance,
Tobi

There is nothing in this code perl couldn't do alone nor awk.
Does x-times mean you give a value and it reads the values from values.exp that number of times, ie. always those 2 over and over and add these to those 2 output files?

If yes and I understood it correct, then here some awk code (5 times):

# awk 'NR==1 {a=$1} {b=$1} END{p1="1 1000"; p2="0.0"; for(x=1; x<=5; x++){print "Processing configuration",x,"..."; print p1,a,p2 >> "test"x".exp"; print p1,b,p2 >> "test"x".exp"}}' values.exp
# cat test1.exp
1 1000 1.2 0.0
1 1000 1.2 0.0
1 1000 1.2 0.0
1 1000 1.2 0.0
1 1000 1.2 0.0
# cat test2.exp
1 1000 1.4 0.0
1 1000 1.4 0.0
1 1000 1.4 0.0
1 1000 1.4 0.0
1 1000 1.4 0.0

In your code you check for some special NR and have some other values as described in the output example, but maybe you can alter/implement it.

Oh, my description wasn't really precise:

I have 136 file (test1.exp, test2.exp, ....) and the values.exp file with 136 lines.
Now i want to use the first line of values.exp and write that to $3 of test1.exp. Then the second line of values.exp and write the value to $3 of test2.exp and so on...

My Perl script is working, but i don't want to change the $3 value in my script for all 136 files manually. So i created values.exp which have all 136 values in 136 lines.

Maybe this description is better...

Ok, I think I got it.
I created 1 file with 4 values to be distributed:

# cat values.exp
1.2
1.4
1.6
1.8

I then created 4 target files with this content, 5 lines where line 3 is the line to be replaced:

# cat test1*
do not touch
do not touch
replace
do not touch
do not touch

Then the line to read the value file and replace with GNU sed's -i option, which alters files in place so you don't have to create a temporary file, inside a while loop:

# COUNT=1; while read A; do sed -i '3s/.*/1 1000 '$A' 0.0/g' test$COUNT.exp; let COUNT=$COUNT+1; done < values.exp

Note the red 3 which is the line number. In your case it would be 2498 I guess.

Here the result:

# cat test1*
do not touch
do not touch
1 1000 1.2 0.0
do not touch
do not touch
# cat test2*
do not touch
do not touch
1 1000 1.4 0.0
do not touch
do not touch
# cat test3*
do not touch
do not touch
1 1000 1.6 0.0
do not touch
do not touch
# cat test4*
do not touch
do not touch
1 1000 1.8 0.0
do not touch
do not touch

Hopefully this is what you are looking for and hopefully you have GNU sed or a similar implementation with -i . Else perl alone can be used with -i or some commands being piped into ex etc.

Thank you so much!!
You spared me a lot of time!!

Dear zaxxon,

thanks again for your help.
Meanwhile I changed some things again and now I would need help again:

I still have a value file, but know my values are in column 2; in column 1 is a corresponding value:

114       1.2
655       1.4
688       1.6

And I still have got my test-files, but the name of the testfiles are not numbered in a row, so i cannot use the $COUNT settings. But the number corresponds to the column 1 in the value file.

test114 file:

do not touch
do not touch
replace
do not touch

test655 file:

do not touch
do not touch
replace
do not touch

So what I want to do is to read the number XXX (i.e. 144, 655,...) in my test file name, find the corresponding value (in column 2) in the value file and replace the "replace"-line in file number655 with the value. I would like to do this for all my files.
So I should and up with:

test114 file:

do not touch
do not touch
1.2
do not touch

test655 file:

do not touch
 do not touch
 1.4
 do not touch

Thanks in advance,
Tobias

If you don't mind temporary files being made:

awk 'FNR==NR{a[$1]=$2;next}FNR==3{f=FILENAME;sub(/^[^0-9]*/,"",f);$0=a[f]}1{print > FILENAME ".new"}' input test*

actually this would be more robust in that it won't kill line 3 if the value wasn't read in the input file (i.e. give it a test999 but had no 999 key)

awk 'FNR==NR{a[$1]=$2;next}FNR==3&&(f in a){$0=a[f]}FNR==1{f=FILENAME;sub(/^[^0-9]*/,"",f)}1{print > FILENAME ".new"}' input test*

Unfortunately, your code isn't working for me.

You could please explain the commands?
Does FILENAME refer to value file?
Should it be written with quotes?

Thanks again

Try:

while read nr val ; do 
  sed -i.bak "s/^replace$/$val/" "test$nr"
done < infile

$val should not contain forward slashes...

I can not use "read" because in my value.file there are a lot of lines between the lines i want to read out:

114 1.2
.
.
233 2.8
.
.
665 1.4
....

Are there other ways?

Use a case statement to weed out the unwanted lines