Handling 2 files simultaneously with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling 2 files simultaneously with awk
# 1  
Old 09-23-2011
Handling 2 files simultaneously with awk

Hello,
Is it possible to handle data from two different files at once in awk (latest version and platform is Fedora). I found on the net that we cannot nest awk. My requirement is that I have two similar files :
File 1:
Code:
Name: abc
Val = 58
Name: cdf
Val = 1;
..................

File 2:
Code:
Name: abc
Val = 58
Name: cdf
Val = 7;
..................

I have to find the difference between the "Val" filed in both the files and dump the difference in sorted order in another file preferentially using awk. All suggestions are welcome.
# 2  
Old 09-23-2011
Can you post desired output for that sample data?
# 3  
Old 09-23-2011
Desired Output

The required output is:
Code:
abc  58    58     0
cdf  1      7    -6
....................

# 4  
Old 09-23-2011
Code:
awk -F"=|:" 'NR==FNR{x=$2;getline;a[x]=$2+0;next} 
{x=$2;getline;val=$2+0; printf("%-s\t%-3d\t%-3d\t%-3d\n",x,a[x],val,a[x]-val)}' file1 file2

Code:
root@bt:/tmp# cat file1
Name: abc
Val = 58
Name: cdf
Val = 1;

root@bt:/tmp# cat file2
Name: abc
Val = 58
Name: cdf
Val = 7;

root@bt:/tmp# awk -F"=|:" 'NR==FNR{x=$2;getline;a[x]=$2+0;next} 
{x=$2;getline;val=$2+0; printf("%-s\t%-3d\t%-3d\t%-3d\n",x,a[x],val,a[x]-val)}' file1 file2

 abc    58     58     0  
 cdf    1      7      -6

--ahamed
# 5  
Old 09-23-2011
Code:
nawk 'FNR==NR{if($1~/^Name/)f1n=$NF; else f1v[f1n]=$NF+0;next} {if($1~/^Name/)f2n=$NF;else print f2n, f1v[f2n], $NF+0, f1v[f2n] - $NF}' OFS='\t' file1 file2

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 09-23-2011
Code:
awk '/^Name/{n=$2;getline<f}/^Val/{x=$NF+0; getline<f; print n,x+0,$NF+0,x-$NF}' OFS='\t' f=file2 file1

Code:
paste file1 file2| awk '{n=$2; getline; print n,$3+0,$6+0,$3-$6}' OFS='\t'

# 7  
Old 09-23-2011
Code:
awk 'END{for(i in a) print i,a[i],b[i]}{x=$NF;getline;y=int($NF);a[x]=a[x]?a[x] OFS y:y;b[x]=b[x]?b[x]-y:y}' OFS=\\t file[1,2]
abc     58      58      0
cdf     1       7       -6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to delete files on two remote servers simultaneously?

dear all, i'm preparing a script which can do these actions : 1. stop remove server's certain service 2. clean the files on remote servers simultaneously (because lots of files need to be deleted) 3. after files/logs are removed, restart the service again i'm stuck on how to clean remote... (4 Replies)
Discussion started by: tiger2000
4 Replies

2. Shell Programming and Scripting

[SOLVED] Handling multiple files using awk

Hi, I am trying to process 2 files simultaneously using awk satisfying following condition, Both files contain 3 columns. It should take entry from column 1 from first file, look for that entry in file 2 and if found, add column 2 and column 3 from both files and output to third file. For e.g.... (4 Replies)
Discussion started by: muazfarooqaslam
4 Replies

3. UNIX for Dummies Questions & Answers

can I create symbolic links for multiple files simultaneously

Does anybody know how to make symbolic links for multiple files simultaneously? Often times I need make symbolic links for multiple files with some common pattern (just like "*.jpg"). Is there a way to avoid making symbolic link for each of them one by one... Thank you! (6 Replies)
Discussion started by: danieladna
6 Replies

4. Shell Programming and Scripting

handling asterix in AWK

I have a file like below. colA^col2^col3^col4^col5 aa^11^aaa^a1a^111^aa* bb*^22^bbb*^bb2^222^bb cc^33^ccc*^3cc^333^ccc dd^44^d*dd*^d4d^444^ddd ee^55^e*ee^e5e*^555^e*e NOTE: '^' is the field separator. I need to get the output as colA^col2^col3^col4^col5 aa^11^aaa^a1a^111^aa... (5 Replies)
Discussion started by: rinku11
5 Replies

5. Shell Programming and Scripting

handling multiple files using awk command and wants to get separate out file for each

hai all I am new to the world of shell scripting I wanted to extract two columns from multiple files say around 25 files and i wanted to get the separate outfile for each input file tired using the following command to extract two columns from 25 files awk... (2 Replies)
Discussion started by: hema dhevi
2 Replies

6. Shell Programming and Scripting

Looping through 2 files simultaneously

Hi all, I'm having a problem with a script which should ultimately provide a filename by reading a value from file1 and file2 then join together. I'm planning to use a loop/ loops to get the values out of both files and create a single string unfortunately the code currently treats the second... (7 Replies)
Discussion started by: chris01010
7 Replies

7. Shell Programming and Scripting

renaming multiple files simultaneously

Hi , I have a large no of files which all end in .asp.htm extension . But for proper navigation between the pages I need to rename all those files as .asp only . How can it be done ? (4 Replies)
Discussion started by: nshailesh
4 Replies

8. Shell Programming and Scripting

Tailing 2 or more log files simultaneously PERL

Hi, I am having issue where I have to tail 3 log files continuously (forever) and while I am reading the files , parse them and shove the data into DB. I can do this with one file totally fine but how can I read 3 files at the same time? I am not really looking for code (but would be nice) but... (3 Replies)
Discussion started by: Dabheeruz
3 Replies

9. Shell Programming and Scripting

simultaneously create three empty files?

I can't get touch to simultaneously create three empty files file1, file2, file3. I tried:$ touch filebut all I got was one file:$ fileWhat did I do wrong? (4 Replies)
Discussion started by: na5m
4 Replies

10. Shell Programming and Scripting

How to parse 2 files simultaneously

Say I have 2 files of 2 rows of 3 columns each file1: cat catdata1 catdata2 dog dogdata1 dogdata2 file2: cat catdata3 catdata4 dog dogdata3 dogdata4 and I need to combine both files so that is appears like: cat catdata1 catdata2 catdata3 catdata4 dog dogdata1 dogdata2 dogdata3... (8 Replies)
Discussion started by: Awanka
8 Replies
Login or Register to Ask a Question