awk to compare 2nd and 3rd field and print the differences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to compare 2nd and 3rd field and print the differences
# 8  
Old 10-09-2012
Yes, the same input file needs to gets read twice and is therefore specified twice on the command line...
# 9  
Old 10-09-2012
@ Scuritizer.. Thanks it looks fine. i will run it on the entire record and let you know .
does this one liner ignore the first line of the file. ( i guess you have omitted it keeping the column header in mind )
# 10  
Old 10-09-2012
awk -f a.awk infile

Where a.awk:
Code:
NR==1 {
 p1=index($0,"col 2");
 p2=index($0,"col 3");
}
NR > 1 {
 v1=substr($0, p1, p2-1);
 sub("^ *", "", v1);
 sub(" *$", "", v1);
 if (length(v1)>0) a[ac++]=v1;
 v2=substr($0, p2);
 sub("^ *", "", v2);
 sub(" *$", "", v2);
 if (length(v2)>0) b[v2]=v2;
}
END {
 for (i=0; i<ac; i++) if (length(b[a[i]])<1) print a[i];
}

# 11  
Old 10-09-2012
Quote:
Originally Posted by chidori
@ Scuritizer.. Thanks it looks fine. i will run it on the entire record and let you know .
does this one liner ignore the first line of the file. ( i guess you have omitted it keeping the column header in mind )
Yes, && FNR>1 keeps the value on the first line of the file from being printed. You can leave that out if the actual file does not contain a header...
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 10-09-2012
@scrutinzer
Thanks for your inputs.. can you please explain the oneliner Smilie
# 13  
Old 10-09-2012
Sure,

NR==FNRWhen the first file is being read (only then are FNR and NR equal)
A[$3]create an (associative) array element with the third field as the index
next start reading the next record
!($2 in A) && NF>2 && FNR>1(while reading the second file, which in this case is the first file for the second time) if the second field is not in the array of recorded third fields, and there are more than 2 fields and the linenumber is higher than 1 (we are past the header line).
{print $2} print field 2
infile infileread infile followed by infile

Hope this helps
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

2. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

3. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

4. Shell Programming and Scripting

only print line if 3rd field is 01

Similar question... I have a space delimited text file and I want to only print the lines where the 3rd word/field/column is equal to "01" awk '{if $3 = "01" print $0}' something like this. I meant to say: only print line IF 3rd field is 01 (2 Replies)
Discussion started by: ajp7701
2 Replies

5. Shell Programming and Scripting

print the last line of an recurring pattern on the 3rd field

How can i awk/sed to print the last line of an recurring pattern on the 3rd field? Input lines: 123456.1 12 1357911 11111.1 01 123456.2 12 1357911 11111.2 02 123456.3 12 1357911 11111.3 03 123456.4 12 1357911 11111.4 04 123456.5 12 1357911 11111.5 05 246810.1 12 1357911 22222.1 01... (4 Replies)
Discussion started by: ux4me
4 Replies

6. Shell Programming and Scripting

Deleting every 3rd field using awk

I have a file whose format is like the following 350,2,16.2,195,2,8.0 every 3rd column of this file should be deleted. How can i achieve this tried with the following iostat -D -l 2 | /usr/xpg4/bin/awk ' NR>2 { for (i=0;i<=NF;i++)if(i%3==0)$i=""};' but no luck (3 Replies)
Discussion started by: achak01
3 Replies

7. Shell Programming and Scripting

print line if 2nd field exists in text

2 files, first one has 3 fields seperated by ||| and 2nd one is plain text. I want to copy the lines from the first file if the 2nd field is present anywhere in the text file. This is what I've tried, but I'm new to awk and shell scripting in general so it's kinda broken. #!/bin/awk -f BEGIN... (15 Replies)
Discussion started by: FrancoisCN
15 Replies

8. Shell Programming and Scripting

awk NR==FNR compare 2 files produce a 3rd

hi, i have two files, both with 3 columns, the 3rd column has common values between the two files and i want to produce a 3rd file with 4 columns. file 1 a, ,b c file 2 a, b ,d I want to compare the 3rd value and if a match print to file 3 with the 3 columns from the first file... (11 Replies)
Discussion started by: borderblaster
11 Replies

9. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies

10. Shell Programming and Scripting

Compare dates in a field and print the latest date row

Hi, I need a shell script which should find the latest date in the field of file and print that line only. For eg., I have a file /date.log Name Date Status IBM 06/06/07 close DELL 07/27/07 open DELL 06/07/07 open : : : From... (1 Reply)
Discussion started by: cvkishore
1 Replies
Login or Register to Ask a Question