DEV Community 👩‍💻👨‍💻

Discussion on: Fun with Bash

math2001 profile image
Mathieu PATUREL • Edited on

Cool command! You could remove the head bit though with awk.

awk 'condition { command }'

By default the condition is NUL, which matches every line (so the command is run on every line). You have access to different variable (in both the condition and the command I think), one of them being NR for the line number.

So, if you set the condition to NR!=1, it'll run on every line different than 1 (which is the first one).

So, your command can be shortened from:

awk -F , '{print $1, $3-$2}' data.csv | tail -n +2 | sort -r -n --key=2 | column -t


awk -F , 'NR!=1{print $1, $3-$2}' data.csv | sort -r -n --key=2 | column -t


I learned a bunch otherwise, so thanks!

quii profile image
Chris James Author

Thanks for the improvements!