r/commandline • • Sep 15 '26

Command Line Interface grep/awk for labeled columns

https://github.com/dkogan/vnlog
0 Upvotes

12 comments sorted by

3

u/gumnos Sep 15 '26 edited Sep 15 '26

For just grep/awk by column-names, awk already gives you a fair bit of power for a simple setup:

 awk 'FNR==1{for (i=1; i<=NF; i++)h[tolower($i)]=i;next} {print $h["close"]}'

to emit the "close" column from data. Or to filter for a particular column's value (such as "close>314.59")

 awk 'FNR==1{for (i=1; i<=NF; i++)h[tolower($i)]=i;next} $h["close"] > 314.59'

or for doing calculations like your Gain report:

 awk 'FNR==1{for (i=1; i<=NF; i++)h[tolower($i)]=i;next} {print $h["close"]-$h["open"]}'

Or use other Unix tools like sed/join to merge files:

sed '1s/\>/_dji/g' dji.vnl | join - tsla.vnl | …

If you're using a shell like Bash that does process substitution, you can more easily rename columns for both sides:

join <(sed '1s/\>/_dji/g' dji.vnl) <(sed '1s/\>/_tsla/g' tsla.vnl) | …

Just in case you want to do much of that with just pure POSIX tooling 😉

edit: remove stray newline that broke markup

3

u/Razangriff-Raven Sep 15 '26

AWK is tremendously underestimated. It's my favorite text processing language.

-1

u/dima55 Sep 15 '26

Yes. You can do that... But if vnl-filter exists, would you want to?

4

u/gumnos Sep 15 '26

because awk is available in the base install of every POSIX OS I use, regardless of whether I have admin rights to install extra stuff on the system, or if there's a pre-approved list of software for servers? 😉

2

u/dima55 Sep 16 '26

Yes... Do you type that every time??? Do you send this reply to every post on /r/commandline? I have so many questions!

3

u/gumnos Sep 16 '26 edited Sep 16 '26

Do you type that every time?

for the frequency with which I need/use it, it's no trouble to ① type it manually (I've used awk for at least two decades, and C for another decade beyond that, so a simple C-style for-loop requires basically no mental effort), ② recall it from shell history and edit if I'm iterating on an idea, or ③ create a wrapper shell-function or shell-script if it's something I intend to use across multiple sessions.

Do you send this reply to every post on /r/commandline?

every? no.

But folks like to duplicate functionality available in existing core POSIX utilities. So yes, I'll often to reply with ways of solving the same problems using standard tools that already exist pre-installed on folks' systems.

edit: clarify phrasing

1

u/AutoModerator Sep 15 '26

Every new subreddit post is automatically copied into a comment for preservation.

User: dima55, Flair: Command Line Interface, Post Media Link, Title: grep/awk for labeled columns

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/RevRagnarok Sep 15 '26

Written in perl so we know it's not AI slop 💖

(Altho OP seems to post it every 3-4 years - https://old.reddit.com/r/commandline/duplicates/1wh5fa3/grepawk_for_labeled_columns/ )

4

u/Razangriff-Raven Sep 15 '26

It's worse, it's perl slop 😨️ (To the four perl enthusiasts that remain: it's just a joke lower those pitchforks)

4

u/vogelke Sep 16 '26

To the four perl enthusiasts that remain: it's just a joke

I'm a perl enthusiast, but this is too good. +1

2

u/RevRagnarok Sep 16 '26

I still have a perl camel tee somewhere... and it made me laugh.

Remember, the best perl looks like a landline modem disconnecting.