How to cut by multiple spaces in Linux?
Posted on In QAHow 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}'