Finding data items in one field that contradict data items in another field
polydesmida.info
polydesmida.info
e.g. this is one of the examples - a kindof sanity check on two fields in a tab separated file to see if one is less than the other. The fields are identified by their position (31, 33 etc)
awk -F"\t" 'NR>1 && $31!="" && $33!="" && $33>$31' fish | wc -l
Surely much better to just import it into a database and do the analysis in SQL. The SQL equivalent of the above would be something like: SELECT *
FROM FishSpecimenData
WHERE MinDepth > MaxDepth
AND MinDepth is not null AND MaxDepth is not null
If you're worried about type conversions while importing into SQL, just import everything as a varchar. You've still got a fairly easy job to compare the numbers: SELECT *
FROM FishSpecimenData
WHERE Cast(MinDepth as int) > Cast(MaxDepth as int)
AND MinDepth is not null AND MaxDepth is not null
AND IsNumeric(MinDepth) = 1 and IsNumeric(MaxDepth) = 1
edit: To be fair, on this page https://www.polydesmida.info/cookbook/index.html the author explains the rationale for using command line tools:I'm a retired scientist and I've been mucking around with data tables for nearly 50 years. I started with printed columns on paper (and a calculator) before moving to spreadsheets and relational databases (Microsoft Access, Filemaker Pro, MySQL, SQLite). In 2012 I discovered the AWK language and realised that every processing job I'd ever done with data tables could be done faster and more simply on the command line. Since then my data tables have been stored as plain text and managed with GNU/Linux command-line tools, especially AWK
So I guess the point of the blog is to promote that approach. Fair enough.
It strikes me as much easier than having to figure out Awk, but that's assuming most people are more likely to know SQL and/or use it in the future.
I mean, I'd be happy to try and solve the problem with Awk because it's been high on my list of things to learn, but SQL does seem like an easier solution in most cases.
I learned awk a few years ago and it does not have equivalent when the only thing you want to do is to consume column-based text and do some simple processing: it is simple and incredibly fast.
Additionally, these data presumably live in a database at the museums they're from. They put them on the web as CSV (or similar) so that the public can see them, but the museum IT guy would end up making the eventual changes in their tracking system anyway.
I've always been of the opinion that you're better served by learning Perl and using it. The syntax for quick things is very similar to Awk, but you also get all of CPAN to back you up in case you need to do something more complex.
For example, real CSV parsing, HTML parsing with queryselector access, complex record destructuring and/or inner-record decompression, and sprintf (among many others) for for output.
I still do a lot of cat / grep / cut / sort / uniq type commands, but whenever I have need of a little complexity, Perl is definitely my go to tool. I think it's already installed in most common installation choices for most distros.
I'm sure there are issues with this, but it seems pretty sane. Your sample query would read _something_ like:
q 'SELECT * FROM FishSpecimenData.csv WHERE c33 > c31 AND c31 is not null AND c33 is not null'
Once the confusion lifted I could enjoy the read.
Fixing your health issues could cost more than the savings you get from living in that shack.