5 ms·
Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs! Like: printing all bu
by chaps 1y ago
Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs!
Like: printing all but one column somewhere in the middle. It turns into long, long commands that really pull away from the spirit of fast fabrication unix experimentation.
jq and sql both have the same problem :)
- thrwwy9234 1y ago$ echo "one two three four five" | awk '{$3="";print}' one two four five
- chaps 1y agoOh dang, that's good.
- shawn_w 1y agoAs long as you don't mind the extra space in the middle.
- fragmede 1y agoif you do: sed 's/ / /g'
- lucb1e 1y agoOr sticking with awk, I have this bash alias to remove excess whitespace that is just: awk '{$1=$1};1'
- fuzztester 1y agowhat does the 1 at the end do? make awk print all lines? I'm a bit rusty with my awk.
- thrwwy9234 1y agoIt’s a condition. 1 is true-ish, so it’s a condition that is always true. The default action in awk is print, so it’s the same as: '{$1=$1}1{print}' Or the same as '{$1=$1}{print}' Since the default condition is true. But 1 is shorter than {print}.
- chaps 1y agoOften times I don't! Entirely depends on what I'm doing. #1 thing off the top of my head is to remove That One Column that's a bajillion characters long that makes exploratory analysis difficult.
- saghm 1y agoI wonder if it's possible to encode the backspace character in the replacement string?
- ZoomZoomZoom 1y agoNo problem, there's \b. echo "one two three four five" | awk '{$3="\b"; print}' Inserts the backspace character (^H), which you can then remove with [global] substitution: awk '{$3="\b"; sub(/\32\b/, ""); print}' You can, of course, use an arbitrary sentinel value for the field to be deleted. Should work in gawk and BWK awk.
- mauvehaus 1y ago...And once you get away from the most basic, standard set of features, the several awks in existence have diverging sets of additional features.
- chaps 1y agoThings are already like that, friend! We have mawk, gawk and nawk. But it's fun to think about how we could improve our ideal tooling if we had a time machine.
- SoftTalker 1y ago> awk really shoots itself so much with its lack of features that it so desperately needs Whence perl.
- librasteve 1y agoor raku https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Grammar.pm https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Gra...
- saghm 1y agoI suspect the rationale for Perl is that most Linux systems will probably have it installed already. Installing something you're familiar with is great when you can, but I'm guessing the awk script linked to here was picked more for its ubiquity than elegance.
- chaps 1y agoKinda, but not really. Of the infrastructures I've worked on, not a single one has been consistent in installing perl on 100% of hosts. The ones that get close are usually like that because one high up person really, really likes perl. And they send a lot of angry emails about perl not being installed. Within infrastructures where perl is installed on 95% of hosts, that 5% really bites you in the ass and leads to infrastructure rot very quickly. You're kinda stuck writing and maintaining two separate scripts to do the same thing.
- SoftTalker 1y agoSame with Python. It's mostly available, but sometimes not. With Perl, I find that a base installation is almost always available, but many packages might not be.
- chaps 1y agoI dunno about that. IME, python is much, much more universally installed on the hosts I've worked on. Sure, usually it's 2.7, but it's there! I've tended to work on rhel and debian hosts, with some fedora in the mix. (Once had a coworker reject a PR I wrote because I included a bash builtin in a deployment script. He said that python is more likely to be installed than bash, so we should not use bash. These debates are funny sometimes.)
- jcynix 1y ago>awk really shoots itself so much with its lack of features that it so desperately needs! That's why I use Perl instead (besides some short one liners in awk, which in some cases are even shorter than the Perl version) and do my JSON parsing in Perl. This diff -rs a/ b/ | ask '/identical/ {print $4}' | xargs rm is one of my often used awk one liners. Unless some filenames contain e.g. whitespace, then it's Perl again
- ptspts 1y agoThis is much safer: xargs -d '\n' rm -f --
- jcynix 1y agoSure, but my example was just that and I actually use /identical$/ as the pattern. Sorry for the typo. And I use this "historic" one liner only when I know about the contents of both directories. As soon as I need a "safer" solution I use a Perl script and pattern matching, as I said.
- poke646 1y agoIn this case the Perl one-liner would be conceptually identical, the same length, but more performant (no calling out to rm): diff -rs a/ b/ | perl -ane '/identical$/ && unlink $F[3]'
- jcynix 1y agoJust tried to use "-d" and learned that it's a GNUism which isn't available under MacOS, so it's not a portable solution. And neither was it available under BSD 4.3 when I learned about xargs the first time.
- 8n4vidtmkvmk 1y agoI've been using perl instead of sed because PCRE is just better and it's the same regex that PHP uses which I've been coding in for nearly 20 years. I still don't actually know perl, but apparently Gemini does. It wrote a particularly crazy find and replace for me. Never got around to using or learning awk. Only time I see it come up is when you want to parse some tab delimited output
- toddm 1y agoUs old UNIX guys would likely go for cut for this sort of task: cut -d " " -f1-2,4-5 file.txt where file.txt is: one two three four five and the return is: one two four five