6 ms·
And awk doesn't offers cut's column range selection ;)
by Scarbutt 7y ago
And awk doesn't offers cut's column range selection ;)
- ColinWright 7y agoI'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
- kseistrup 7y agoI wrapped awk in a thin shell script, f, so that you can say “f N” to get “awk '{print $N}'” and “f N-M” (where either of the end points are optional) to do what cut does, except it operates on all blanks. Repo has a few other shortcuts, too: ⌘ https://github.com/kseistrup/filters https://github.com/kseistrup/filters
- jerf 7y agoAbsolutely. Everything comes with costs & benefits. But I'm not sure I've, in my entire 23-year professional programming career, ever encountered a fixed-width text format in the wild. I've used cut even so for places where by coincidence the first couple of columns happened to be the same size, but that's really a hack. Obviously, other people have different experiences which is why I quality it so. (I only narrowly missed it at the beginning, but I started in webdev, and we never quite took direct feeds from the mainframes.) But I don't think it's too crazy to say UNIX shell, to the extent it can be pinned down at all, works most natively with variable-length line-by-line content.
- buzer 7y ago> But I'm not sure I've, in my entire 23-year professional programming career, ever encountered a fixed-width text format in the wild. SAP comes to mind. I think it does support various different formats, but for reason or another fixed-width seemed to be some kind of default value (that's what I usually got when I asked for SAP feed at least, but that was years ago).
- btilly 7y agoI can confirm that they are not a common problem. Admittedly I have encountered fixed-width text formats in the wild. But the last such occasion was about 15 years ago. (It was for interacting with a credit card processor to issue reward cards.)
- F-0X 7y agoWithin my first year of professional development, I encountered several fixed-width files I needed to read and write. I suppose exposure depends a lot on the specific industry.
- hnick 7y agoAlso big mainframe users (banks, insurance) often send fixed width data to us.
- 7thaccount 7y agoSeveral scientific data formats in my industry have fixed width columns that traces back to the era of punch cards