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
# 1  
Old 10-09-2012
awk to compare 2nd and 3rd field and print the differences

need a one liner to compare 2nd and 3rd field and print values that are not matched in 2nd field

Input
Code:
col 2    col 3
1.1.1.1  11.11.11.11
8.8.8.8   0.0.0.0
3.3.3.3   2.2.2.2
7.7.7.7   3.3.3.3
5.5.5.5   1.1.1.1
          4.4.4.4
          6.6.6.6
          9.9.9.9

output
Code:
7.7.7.7 
5.5.5.5 
8.8.8.8

Please help me out

P.S. Both colum length differ and there are around 1000+ records
# 2  
Old 10-09-2012
Without seeing your actual data, this will print column2 if it is not equal to column3:
Code:
awk '{if($2!=$3){print $2}}' filename

# 3  
Old 10-09-2012
Hi ,

That would work only when values to be compared fall in the same row right.!
# 4  
Old 10-09-2012
Correct.
# 5  
Old 10-09-2012
mine is different.. the values are scattered randomly
# 6  
Old 10-09-2012
Try:
Code:
awk 'NR==FNR{A[$3]; next} !($2 in A) && NF>2 && FNR>1 {print $2}' infile infile

# 7  
Old 10-09-2012
@Scurtinizer

Thanks for the response. But my input is one single file where i have to compare 2nd and 3rd field and print values of 2nd field that are not in 3rd field
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