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?

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}'

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *