top | item 45934129

(no title)

sudahtigabulan | 3 months 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

discuss

order

lelanthran|3 months ago

Cut has flags to extract byte or character ranges.

You don't have to use fields.

sudahtigabulan|3 months ago

Can these flags be used to extract the N-th column (say, the size) of every line from ls -l output?