3 ms·
> That’s typical usage of Awk, where you use it in place of cut because you can’t be bothered to remember the right flags for cut. Even you remember the flags,
by sudahtigabulan 11mo ago
> That’s typical usage of Awk, where you use it in place of cut because you can’t be bothered to remember the right flags for cut.
Even you remember the flags, cut(1) will not be able to handle ls -l. And any command that uses spaces for aligning the text into fixed-width columns.
Unlike awk(1), cut(1) only works with delimiters that are a single character. Meaning, a run of spaces will be treated like several empty fields. And, depending on factors you don't control, every line will have different number of fields in it, and the data you need to extract will be in a different field.
You can either switch to awk(1), because its default field separator treats runs of spaces as one, or squeeze them with tr(1) first:
ls -l | tr -s' ' | cut -d' ' -f3
- lelanthran 11mo agoCut has flags to extract byte or character ranges. You don't have to use fields.
- sudahtigabulan 11mo agoCan these flags be used to extract the N-th column (say, the size) of every line from ls -l output?
- lelanthran 11mo agoYes. $ ls -l | cut -c 35-41 22 4096 4096 4096 4096 4096 4096 68 456 690 7926 8503 19914
- sudahtigabulan 11mo agoThis is what I get: ls -l | cut -c 35-41 6 Nov 1 6 Nov 6 Nov 1 6 Nov 1
- lelanthran 11mo agoWell, sure. I said it did character ranges so you don't have to use fields. What were you expecting? That your character ranges in ls would match mine?
- sudahtigabulan 11mo ago> What were you expecting? That your character ranges in ls would match mine? I would expect the command to work in any directory. Try a few different directories on your computer and you'll see that it won't work in some of them.
- lelanthran 11mo ago> I would expect the command to work in any directory. But ... why expect that? That's not what "character ranges" mean. I mean, I was only trying to clarify that `cut` is not limited to fields only.