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:
(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
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