Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a process that generates output mostly in lexicographically sorted order according to a (timestamp) field, but occasionally the lines will be output in the wrong order:

2014-08-14 15:42:02.019220203 ok
2014-08-14 15:42:03.523164367 ok
2014-08-14 15:42:04.525655832 ok
2014-08-14 15:42:06.523324269 ok
2014-08-14 15:42:05.930966407 oops
2014-08-14 15:42:07.643347946 ok
2014-08-14 15:42:07.567283110 oops

How can I identify each location where the data are "unsorted"?

Expected output (or similar):

2014-08-14 15:42:05.930966407 oops
2014-08-14 15:42:07.567283110 oops

I need a solution that works as the data are generated (e.g. in a pipeline); it's less useful if it only operates on complete files. sort --check would be ideal but it only outputs the first point of disorder; I need a full listing.

share|improve this question
up vote 4 down vote accepted
awk 'NR>1 && $0"" < last; {last=$0}'

Prints the lines that sort before the preceding line. The $0"" is to force lexical comparison (on the output of seq 10 it would spot 10 as sorting before 9).

share|improve this answer

I think that shell string comparisons should respect lexicographical order (according to the current locale, of course) - so perhaps you could do something like


while IFS= read -r line; do 
  [[ "$line" < "$last" ]] && printf '%s\n' "$line"
done < <(your process)
share|improve this answer
On the output of seq 1000000, I find it's about 50 times as slow as the gawk equivalent (12x for ksh93, 20x for zsh and mksh, and mawk is twice as fast as gawk). IMO at least, using while read loops to process text is bad practice. At least here, you didn't fall into the usual pitfalls though. – Stéphane Chazelas Aug 14 '14 at 15:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.