3 ms·
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 (inclus
by ColinWright 7y ago
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?
- cauthon 7y agoI don't think so. I think they're referring to cut's ability to select an arbitrary range of columns, e.g. `cut -f 2-7` to select the 2nd through 7th columns, while awk requires you to explicitly enumerate all desired columns, i.e. `awk '{print $2, $3, $4, $5, $6, $7}'
- deleted 7y ago[deleted]
- Scarbutt 7y agoWhat cauthon said, cut lets you select range of fields with a simple option, awk doesn't, ex(csv line): 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.
- ColinWright 7y agoOK, understood. And yes, in my experience AWK is less good at that, cut would definitely be the right tool. It doesn't detract from the point at hand - which is perfectly valid - but it's worth noting that there's a confusion here with regards the terminology: "fields" vs "columns". I thought they were referring to "columns of characters" whereas the added explanations[0] are about "columns of fields". That makes a difference. But as I say, yes, I agree that to select a range of columns of fields, especially several fields from each line, is definitely better with cut. [0] https://news.ycombinator.com/item?id=22109551 https://news.ycombinator.com/item?id=22109551