How to cut by multiple spaces in Linux?

How to cut by multiple spaces in Linux?

For example, if I get a string like

a b  c      d

where the spaces among the fields are not decided.

How to get the four fields a, b, c and d out?

related to an answer for: `cut` using a string as the delimiter?
asked Oct 16, 2015 by anonymous

1 Answer

Here, we take getting 'b' (the 2nd field) as the example.

You can first squeeze the repeated spaces by tr with -s option and then use cut as normal:

tr -s ' ' \
| cut -d ' ' -f 2

Another way is to use awk directly:

awk '{print $2}'
answered Oct 16, 2015 by Eric Z Ma (44,280 points)
edited Jul 18, 2017 by Eric Z Ma

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...