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.
#!/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?
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):
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.
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.
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: