Earlier quoted context omitted.
And awk doesn't offers cut's column range selection ;)
I'm an expert on neither AWK nor cut , but AWK allows you to select a field, or the entire line, and then substrings within those. Select characters 3-6 (inclusive) in the second field: $ echo Testing LengthyString | awk '{print substr($2,3,4)}' > ngth If you want to select columns from the entire line, then: $ echo Testing LengthyString | awk '{print substr($0,3,4)}' > stin Is that what you meant?
Why Learn Awk? (2016)
61–70 of 246 posts
Re: Why Learn Awk? (2016)
#62Earlier quoted context omitted.
And awk doesn't offers cut's column range selection ;)
I'm an expert on neither AWK nor cut , but AWK allows you to select a field, or the entire line, and then substrings within those. Select characters 3-6 (inclusive) in the second field: $ echo Testing LengthyString | awk '{print substr($2,3,4)}' > ngth If you want to select columns from the entire line, then: $ echo Testing LengthyString | awk '{print substr($0,3,4)}' > stin Is that what you meant?
cut -d, -f10-30
(selects from field 10 to 30)Not saying this can't be written in awk with more code, but we were talking about field selection ergonomics.
Re: Why Learn Awk? (2016)
#63https://ferd.ca/awk-in-20-minutes.html
Also, a handy trick is to combine awk and cut. For example I had a log line that had a variable amount of columns just in one field, but immediately after the field was a comma. I cut based on the comma:
cut -d, -f1,2
and then awk'd the last column:
cut -d, -f1,2 | awk '{ print $2" "$5" "$NF }'
So, sometimes awk and cut can help each other.
Re: Why Learn Awk? (2016)
#64Hmm. I'm sure this question will induce a flamewar of practical "#NeverAwk"-ers fighting toolbelt bloat, versus tech-hoarding AWK apologists arguing against throwing something out given if fills . Here's the thing: these arguments all too commonly focus on subjective notions of "simplicity", and toy examples divorced from actual common practise, and or solid comparable benchmarks . Show me a range of practical exampl…
I like to tell the story of when I was doing some genetics data wrangling and spent three days writing some perl code and I kept failing, then sent one of the researchers an email and he suggested an awk method and I turned 3 pages of perl into an awk one liner that works just fine. Now, its probably because I don't know perl very well, but as an ops type who doesnt have the classical dev/cs education, tools like it…
And you have immediate access to so many useful modules (csv, json, xml) and can easily extract code fragments into functions.
You can also execute shell-like commands with subprocess.check_output() without ever worrying again about escaping strings or accidentally splitting them at spaces or whatever.
Clever one liners are difficult to comprehend. It's better to break them up to a few variable assignments with descriptive, long names without abbreviations.
Re: Why Learn Awk? (2016)
#65Because for some bizarre reason, "cut" doesn't ship with any decent column selection logic that is the equivalent of awk's $1, $2, etc., even in 2020. That's like 90% of my use of awk right there. I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Posted partially so the Internet Correction Squad can froth at the mouth and set me straight, because I'd love to be showed to be wrong here.
And awk doesn't offers cut's column range selection ;)
Repo has a few other shortcuts, too:
Re: Why Learn Awk? (2016)
#66Earlier quoted context omitted.
Well... no, it doesn't, obviously, we can take a quick look at gawk to confirm this. But, just as we can't say "awk offers the -b flag to process characters as bytes", we can't really say that cut offers any extensions not defined in the standard. An implementation could, sure. I'd prefer that it didn't, writing conformant shell scripts is hard enough.
Before standards happen, creative developers are free to use their imagination and come up with useful features. Then someone makes up a standard, and from thereon progress is halted. The only way you grow new features and functionality is through design-by-committee, if people aren't making extensions that would one day make it to the next revision. I think it is ridiculous. Tools should improve, and standards shoul…
When I'm looking to do something more complicated, I'd rather look to tools from outside the POSIX standard. The good ones give me less fuss, because they can typically be installed on any POSIX-compliant OS, which is NBD, whereas extensions to POSIX tools tend to create actual portability hassles.
Re: Why Learn Awk? (2016)
#67Earlier quoted context omitted.
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Does `cut -f2` not work? My complaint with cut is that you can't reorder columns (e.g. `cut -f3,2` ) Awk is really great for general text munging, more than just column extraction, highly recommend picking up some of the basics Edit to agree with the commenters below me: If the file isn't delimited with a single character, then cut alon…
It does not. Compare: $ echo ' 1 2 3' | cut -f2 1 2 3 $ echo ' 1 2 3' | cut -f2 -d' ' $ echo ' 1 2 3' | awk '{print $2}' 2 "-f [...] Output fields are separated by a single occurrence of the field delimiter character."
FS = "[0-9]"Re: Why Learn Awk? (2016)
#68Needs a '(2016)' at the end of the title.
Re: Why Learn Awk? (2016)
#69Earlier quoted context omitted.
> Does `cut -f2` not work? Most utilities don't use a tab character as separator, and that's what cut operates to by default. Can't cut on whitespace in general, which is what's actually useful, and what awk does. Only way to get cut to work is to add a tr inbetween, which is a waste of time when awk just does the right thing out of the box.
> which is a waste of time when awk just does the right thing out of the box. Agree in general. Only exception I'd make to this is when you're selecting a range of columns, as someone else mentioned elsewhere in the thread. I typically find (for example) `| sed -e 's/ \+/\t/g' | cut -f 1,3-10,14-17` to be both easier to type and easier to debug than typing out all the columns explicitly in an awk statement.
| tr -s ' ' '\t'Re: Why Learn Awk? (2016)
#70Because for some bizarre reason, "cut" doesn't ship with any decent column selection logic that is the equivalent of awk's $1, $2, etc., even in 2020. That's like 90% of my use of awk right there. I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Posted partially so the Internet Correction Squad can froth at the mouth and set me straight, because I'd love to be showed to be wrong here.
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. Does `cut -f2` not work? My complaint with cut is that you can't reorder columns (e.g. `cut -f3,2` ) Awk is really great for general text munging, more than just column extraction, highly recommend picking up some of the basics Edit to agree with the commenters below me: If the file isn't delimited with a single character, then cut alon…
As others have pointed out, no. It should! (Said the guy sitting comfortably in front of his supercomputer cluster in 2020. No, I don't do HPC or anything; everything's a supercomputer by the time that cut was written's standards.) But it doesn't. Going out on a limb, it's just too old. Cut comes from a world of fixed-length fields. Arguably it's not really a "unix" tool in that sense.
"highly recommend picking up some of the basics"
I have, that's the other 10%. I've done non-trivial things with it... well... non-trivial by "what I've typed on the shell" standards, not non-trivial by "this is a program" standards.