Quantcast
Channel: Find out if lines of a file is sorted - Ask Ubuntu
Viewing all articles
Browse latest Browse all 3

Find out if lines of a file is sorted

$
0
0

I have a large text file, where each line consists of three numbers, except for a few comment lines that all start with a hashtag (#). I want to verify that the non-comment lines are sorted numerically. Is there a good way to do this?

I assume I have to extract all the non-comment lines with something like grep -ve \# - butwhere do I pipe this to verify that the output is sorted?

Note: I am not looking for a way to sort the file, but rather to verify that it already is sorted (to verify output the output of a program of mine). Unfortunately, this isn't as common as wanting to sort the contents of a file, or (even more so) wanting to list the contents of a folder in a particular order (alphabetically or by size, for example), so Google has very little for me...

Clarification: The numbers in the file are real, usually on exponential form. I want them sorted in numerical order, which for example means that 0.11000E+02 > 0.90000E+01 > 0.15000E-01.

If it simplifies any, I know that if the file matches what I want, the first two columns will be sorted if counted in pairs - in other words, if the file is valid the third column doesn't have to be considered at all.

Formally, you can express it like this: if x1 and y1 are the first two numbers on one line, and x2 and y2 are the first two numbers on another line, then (x1,y1)>(x2,y2)iff(x1>x2) || (x1==x2 && y1>y2). (x1,y1)>(x2,y2) here means that the line with x1 and y1 should be considered larger, the line with x2 and y2, and (x1,y1) should appear below(x2,y2) in the file.

Sample input:pastebin
I expect the above file to be considered as sorted, but if any two lines (that are not comment lines) are switched, the file is no longer sorted. Note that lines can have leading whitespace.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>