Frage

OK so I am not sure that gawk is the best tool here, so if anyone has an easy way of doing this using perl, sed, uniq I will be glad to use it. I am trying to filter a set of data that looks like this:

"1" "ARI201304010" "SLN" 1 0 0 1
"2" "ARI201304010" "SLN" 1 0 1 1
"3" "ARI201304010" "SLN" 1 0 1 3
"4" "ARI201304010" "SLN" 1 0 1 0
"5" "ARI201304010" "SLN" 1 0 2 1
"6" "ARI201304010" "SLN" 1 1 0 1
"7" "ARI201304010" "SLN" 1 1 0 0
"8" "ARI201304010" "SLN" 1 1 1 0
"9" "ARI201304010" "SLN" 1 1 2 2
"10" "ARI201304010" "SLN" 2 0 0 0

The 5th element can be only 1 or 0. What I would like to print is every last occurance of a value on the 5th field. Only print if the 5th field does not mach the value in the line before.

War es hilfreich?

Lösung

I think awk is exactly the right tool:

 awk '$5 != last; {last = $5}' last=-1 input

Note that this prints a line 'if the 5th field does not match the value in the line before', but does not 'print every last occurrence of a value'. But I do not fully understand the second to last sentence in your question, so I'm ignoring it since it seems to contradict the last sentence of the question.

If what you want is to print a line only if the 5th field of the next line is different, try:

 awk '$5 !=b && NR>1{print a} {a=$0; b=$5}' input
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top