awk read value from file and paste in other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk read value from file and paste in other file
# 1  
Old 04-12-2012
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:
Code:
1.2
1.4

test1.exp
Code:
1

test2.exp
Code:
2

output:
Code:
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:
Code:
#!/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

Last edited by Franklin52; 04-12-2012 at 08:29 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 04-12-2012
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):
Code:
# 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.

Last edited by zaxxon; 04-12-2012 at 09:17 AM.. Reason: altered code so cycle number is in filenames and added header
# 3  
Old 04-12-2012
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...
# 4  
Old 04-12-2012
Ok, I think I got it.
I created 1 file with 4 values to be distributed:
Code:
# 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:
Code:
# 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:
Code:
# 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:
Code:
# 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.
# 5  
Old 04-13-2012
Thank you so much!!
You spared me a lot of time!!
# 6  
Old 04-17-2012
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:
Code:
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:
Code:
do not touch
do not touch
replace
do not touch

test655 file:
Code:
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:
Code:
do not touch
do not touch
1.2
do not touch

test655 file:
Code:
do not touch
 do not touch
 1.4
 do not touch

Thanks in advance,
Tobias
Moderator's Comments:
Mod Comment How to use code tags

Last edited by tobias1234; 04-17-2012 at 11:22 AM..
# 7  
Old 04-17-2012
If you don't mind temporary files being made:
Code:
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)
Code:
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*


Last edited by neutronscott; 04-17-2012 at 11:30 AM.. Reason: more robust
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

3. Shell Programming and Scripting

Using awk to read one file and search in another file

Hi Forum. I did some google search on what I'm trying to do but I cannot get my code to work correctly. I have 2 files which are very large and I want to read text from file1 and search in file2 - if present, keep the records. I've tried fgrep -f file1 file2 but it is too slow. File1:... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Help required the cut the whole contents from one file and paste it into new file

Hi, First of all sincere apologies if I have posted in a wrong section ! Please correct me if I am wrong ! I am very new to UNIX scripting. Currently my problem is that I have a code file at the location /home/usr/workarea/GeneratedLogs.log :- Code :- (Feb 7, 571 7:07:29 AM),... (4 Replies)
Discussion started by: acidburn_007
4 Replies

5. Shell Programming and Scripting

Paste content of a file to another file and make it as columned

Pls help me on this. I have to 2 files like shown below: File 1 TAIJM AXPKIM BEMGW File 2 PXMPA JYGE IMJP What i want to do is to paste both file to a new file on thir format: File 3 TAIJM PXMPA AXPKIM JYGE BEMGW IMJP I tried cat and print, but it doesn't work. Cn... (6 Replies)
Discussion started by: kingpeejay
6 Replies

6. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

7. Shell Programming and Scripting

read file column and paste it in command

Hi Unix gurus I have a file containing 2 coloumns. I would like to do a script which reads the lines and executes a command like this: command <field1> parameters <field2> some more parameters Please let me know how you would do this without AWK, SED or any other mini language (for special... (5 Replies)
Discussion started by: BearCheese
5 Replies

8. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

9. Shell Programming and Scripting

File Read using awk

Hi, I need to read two colums(4th and 5th) from a file and do some manipulation Input file is 401500 IOC Q 14 14 406200 LC Q 1 1 410124 IOC Q 5 4 410124 LC Q 11 8 410132 IOC Q 230 229 410148 IOC Q ... (3 Replies)
Discussion started by: rejirajraghav
3 Replies

10. Shell Programming and Scripting

Help with awk - read from file

Hi, I've got a file like the following: Starting to process segment 0 (and symmetry related segments) Number of (cancelled) singularities: 0 Number of (cancelled) negative numerators: 0 Segment 0: 5.49secs Starting to process segment 1 (and symmetry related segments) Number of... (7 Replies)
Discussion started by: giorgos193
7 Replies
Login or Register to Ask a Question