5 ms·
awk is irreplaceable on the command line because there's not any other good way to extract columns (awk '{print $2, $3}') or parse/work with numbers. How else w
by kjeetgill 8y ago
awk is irreplaceable on the command line because there's not any other good way to extract columns (awk '{print $2, $3}') or parse/work with numbers. How else would you print a running total for select columns easily? awk '$4>3 { x+=$3; print $2, $3, x}'
It's the only tool that's aware of numbers, unlike grep and sed.
My big new power move for awk is -F. You can use any regex as a field separator! Mind blown!
- v3gas 8y agoI made a tool for columns: https://github.com/vegarsti/csvprint https://github.com/vegarsti/csvprint
- sampo 8y ago> awk '{print $2, $3}' perl -lane 'print "$F[1] $F[2]"' > awk '$4>3 { x+=$3; print $2, $3, x}' perl -lane '$F[3] > 3 && {$x += $F[2], print "$F[1] $F[2] $x"}' Don't know about perl6, though. Edit: Better syntax per comments.
- kjeetgill 8y agoI like the awk better here but I'm sure there's loads more stuff perl could let me do that I can't here. Want to throw a few examples my way? Disclaimer: never learned perl
- acqq 8y agoAdd the "w" (for "warn") make it "-wlane" and read: Scalar value @F[0] better written as $F[0] at -e line 1. Scalar value @F[1] better written as $F[1] at -e line 1. In Perl 5 the array elements of @a are $a[0] $a[1] etc. However @F[0] works too because a "slice" is returned.
- lysium 8y agoShouldn’t that be $F[1] etc.?
- EvilTerran 8y agoOne trick I came up with recently: I found myself needing to add up a column of numbers, but there were so many that I started losing precision to awk's use of floats for all numbers. I wound up augmenting with `dc`[0] - something like (borrowing your example, and working from memory): awk ' BEGIN { print "0" } $4 > 3 { print $3"+p" } ' | dc [0] https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc....
- creatornator 8y agoQuick note: my favorite way to extract columns is: `cut -d' ' -f1, 5`. You won't be getting a running total but for selecting columns I find it useful.