Command like head except instead of showing output by new line, shows output by space

head example:

 λ df -h
Filesystem       Size   Used  Avail Capacity iused    ifree %iused  Mounted on
/dev/disk1s7s1  113Gi  8.9Gi  8.6Gi    51%  355384 90345720    0%   /
/dev/disk1s2    113Gi  3.3Gi  8.6Gi    28%    1743 90345720    0%   /System/Volumes/Preboot
/dev/disk1s4    113Gi   24Ki  8.6Gi     1%       5 90345720    0%   /System/Volumes/VM
/dev/disk1s6    113Gi   63Mi  8.6Gi     1%     660 90345720    0%   /System/Volumes/Update
/dev/disk1s5    113Gi   91Gi  8.6Gi    92%  655534 90345720    1%   /System/Volumes/Data
/dev/disk1s1    113Gi   64Ki  8.6Gi     1%      15 90345720    0%   /Volumes/mnbvcxz    - Data
/dev/disk3s1     58Gi   57Gi  843Mi    99%     209  8636800    0%   /Volumes/Untitled

 λ df -h | head -n1
Filesystem       Size   Used  Avail Capacity iused    ifree %iused  Mounted on

Is there a cmd specifically that will show output of fields by space instead of new line?

A cmd what that will do what this awk cmd does:

Filesystem       Size   Used  Avail Capacity iused    ifree %iused  Mounted on  
| awk -F ' ' '{print$1}'
Asked By: Nickotine


You seem to be looking for cut.

With your example, that would be cut -d' ' -f1.

Answered By: xhienne

Annoyingly, there’s no standard command that will extract ranges of columns separated by a variable amount of whitespace without mangling the spacing:

  • For cut -d ' ' -f 1-2, any single space is a delimiter so for instance " a b" is 4 fields: "", a, "", b.
  • cut -c 1-4 just cuts characters, not fields
  • awk '{print $1, $2}' extracts fields separated by any amount of blanks, ignoring leading and trailing ones as that’s the special behaviour when FS=" " as it is by default, but those fields are output separated by one space character (the default value of OFS).

To extract the first $n (or up to $n) fields, while preserving the whitespace between them, with GNU grep or compatible, one approach is to use:

grep -Eo "^(s*S+){0,$n}"

(beware it discards empty lines)

Now, note that the output of that df command doesn’t follow a pattern that allows extracting data reliably that way as the field values themselves can contain whitespace. See for instance the Mounted on header or some of the mount points. See also how some of the fields are left-aligned and some right-aligned. It’s very hard to extract fields in that condition without hardcoding the field widths (and use cut -c for instance). It’s also why mlr --pprint cut can’t be used here.

If you can, best is to tell the tool that produces the output to print only the columns you want. For instance, with the GNU implementation of df, you can use:

df -h --output=source,size,used

And if you need to post-process that output, look out for options to output in a parsable format. For df, -P helps only slightly, df‘s output is not post-processable reliably.

If on Linux, findmnt, lsblk and GNU stat -f can output the same information as df can in a post-processable way (see for instance the -J option with How to process JSON with strings containing invalid UTF-8 and -c for stat)

Answered By: Stéphane Chazelas
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.