[awk] working with two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] working with two files
# 1  
Old 01-26-2012
[awk] working with two files

Hello guys,
I have little to no experience working with two files in awk and hope you can help me with a problem that may be easy for you to solve...

Code:
awk -v cut1="$var1" -v cut2="$var2" '{split($0, arr1); for(i=1;i<=NF;i++) if (arr1[i] < cut1) print arr1[i], NR, i}' file1 file2

The above code is the basis for what I need to do. I have two cutoffs cut1 and cut2 that I read into awk and two files with the same number of columns and lines.

I need to check a condition if (arr1[i] < cut1) for every element of file1 and if it is true check the same element in file2 for another condition. If both conditions prove true then I need to print out the elements of both files, as well as line and field number.

Am I making any sense here or should I choose an alternative route to a solution? Smilie

Thanks for any help!
# 2  
Old 01-26-2012
I think you should post sample input data and desired output.
# 3  
Old 01-26-2012
cutoffs:
Code:
cut1=1.00000
cut2=5.00000

file1
Code:
3.75289 3.72466 3.72257
3.45011 3.43072 3.44495
3.27445 3.25306 3.27761
3.0973 3.06597 3.09548
2.76982 0.55555 2.76169
2.83836 2.80111 2.81722
2.71083 2.65765 2.66011
2.77565 2.7335 2.67739
2.96274 2.91895 2.88512
2.93073 2.8461 2.89448

file2
Code:
1.1154    0.0141    0.0234
0.0198    0.0258    0.0230
0.0126    0.0214    0.0174
0.0115    0.0143    0.0246
0.0146    9.9999    0.0149
0.0128    0.0143    0.0157
0.0114    0.0158    0.0152
0.0109    0.0248    0.0149
0.0116    0.0211    0.0216
0.0304    0.0247    3.3209

desired output:
Code:
value1: 9.9999 | value2: 0.55555 |  line: 5 | column: 2

output should only be printed if both conditions are met, e.g.: 0.55555 < cut1 and 9.9999 > cut2
# 4  
Old 01-26-2012
Try:
Code:
paste file1 file2 | awk -v cut1="$var1" -v cut2="$var2" '{for (i=1;i<=3;i++){if($i<cut1&&$(i+3)>cut2){print "value1:",$(i+3),"| value2:",$i,"| line:",NR,"| column:",i}}}'

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 01-26-2012
great idea bartus, thanks... this way it works generally for me!

Code:
paste file1 file2 | awk -v cut1="$var1" -v cut2="$var2" '{for (i=1;i<=NF/2;i++){if($i<cut1&&$(i+NF/2)>cut2){print "value1:",$(i+NF/2),"| value2:",$i,"| line:",NR,"| column:",i}}}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

2. Shell Programming and Scripting

Awk: System command not working in awk

Hi, I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help Following is the code awk -F"\t" '{print... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

awk - why this is not working? trying next word!

Hi Experts, Can you please advise , why I am not able to make it work, or why this is not working: I spent quite a lot of time on this figuring out , but not working, file : This is a test file thanks for your reply This is another file again Have a nice day this is a small file... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

awk help : if then else not working!

Hi All, This code is not working when trying with "else" : Am I missing something, uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" )} ;else print "OK" }' Getting this error: syntax error The source line is 1. The error context is { if... (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

awk comparison not working

Can you please help me on belw awk comparsion which doest not work cat employee_list NAME Last-login Jack 03/25/2013 Maneul 03/26/2013 Eric 03/26/2013 Samuel 03/28/2013 loak 03/29/2013 zac 03/29/2013 this is my awk .. it gives me error cat employee_list | awk '(($2=='date... (3 Replies)
Discussion started by: Sara_84
3 Replies

6. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

7. Shell Programming and Scripting

awk not working in script

Hi All, i'm trying awk commnad to read only file names with the help of below command :- ls -l tmp*.txt | awk { 'print $9' } | head -1 The command is working fine :o on command line, but when i trying it in script then its not working $var1=`ls -l tmp*.txt | awk { 'print $9' } | head -1`... (3 Replies)
Discussion started by: doitnow
3 Replies

8. Shell Programming and Scripting

awk not working for me.

Hi All, I have a server.xml file which looks something like. <Connector port="8443" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" disableUploadTimeout="true" acceptCount="100"... (2 Replies)
Discussion started by: nua7
2 Replies

9. Shell Programming and Scripting

Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help. I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1. I... (3 Replies)
Discussion started by: ambroze
3 Replies

10. Shell Programming and Scripting

awk not working as expected with BIG files ...

I am facing some strange problem. I know, there is only one record in a file 'test.txt' which starts with 'X' I ensure that with following command, awk /^X/ test.txt | wc -l This gives me output = '1'. Now I take out this record out of the file, as follows : awk /^X/ test.txt >... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question