I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me by using awk or perl ? Thanks
assuming your input is sorted based on the first column...
nawk -f ray.awk myInputFile
ray.awk:
# if the value in the first column ($1) does not equal the value stored in "prevIDX"
# and we are not at the FIRST record (FNR != 1) [FNR = RecordNumber in the current File]- print the value of "prev"
$1 != prevIDX && FNR != 1 { print prev}
# assign the value of the FIRST field ($1) to a variable "prevIDX"
# assign the entire record/line to the variable "prev"
{ prevIDX=$1; prev=$0 }
# after ALL the records/lines were processed - print the value of the variable "prev"
# the last/dangling record/line
END { print prev }
That's cool!! It works!! You a are brillant!!
But i ' m new to the " prev " and " FNR " term.
Can you explain your code so that i can understand better ?
Hi Summer,
Your code is able to work but some how the sequence of display seems to be jumbled up. " 0004 " should be at the last line but it came to the 1st line.
If i need to capture the 1st occurrence instead that is if column1 of row1 equals to column1 of row2 then the whole line of column1 row1 is being printed out , how can i modify your code below to fulfill this requirement?