Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to compare two fields in different lines with awk? 1

Status
Not open for further replies.

rafquery

Technical User
Dec 16, 2011
2
FR
Hi,

I would like to know if it is possible to compare two fields that are in different lines?

I have a file that looks like this:

a b 145 c d
e f 102 g h
i j 247 k l
m n 102 o p

and I would like to get for example here the two lines that contains "102" in $4.

I first sorted out the file on column 3 to get the lines I want close to each other, and I was thinking to do something like :

x=$3 in line_n, y=$3 in line_n+1
if x==y, print line_x; print line_x+1


So I tried something like:
Code:
sort -k3 file.txt > file_sorted.txt
awk '{x= $3; NR=NR+1; y= $3; if (x==y) {print $0}' file_sorted.txt > wanted_lines.txt

(ps: I know that even if my command worked out, I would obtain only one of the two lines I want (and I don't even know if I would get the line_n or line_n+1...)) :$ :$ :$

In fact, I know that awk will treat the line 1, then the line 2... and I would like it to treat line1&line2, then line2&line3.
But maybe my idea to use awk to do this is not good!

So if you could help me for this it would be so niiice :)


thanks
 
A starting point:
Code:
awk '{x[$3]=$0"\n"x[$3];++n[$3]}END{for(i in x)if(n[i]>1)print x[i]}' file.txt > wanted_lines.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi again!

many thanks PHV !!! it worked really well!! it is so beautiful to get what you want with a simple line!
Can you just tell me if I understand well what you did :

- "x[$3]=$0" : you created a dictionary in which you associated the number in column 3 with the full line in which you found this number (no need to sort if I understood)

- " "\n"x[$3];++n[$3]" : this part I didn't understand very well :)

- "END" : then once you did this for all the lines of the input file "file.txt",

- "for(i in x)" : for all numbers in the dictionary,

-- "if(n>1)print x" : if the number of times you find each number is > 1, you print all the lines that are associated with this number in the output file "wanted_lines.txt"

thanks again :)
 
this part I didn't understand very well
In fact the 2 parts are:
1) x[$3]=$0"\n"x[$3]
2) ++n[$3]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top