Use pipes to squeeze the extra whitespaces and send your data (e.g, in columns.txt
) into cut
:
tr -s ' ' < columns.txt | cut -d" " -f2
In the example data you provided, a single space delimiter puts the data you want in field 5. However, if the first column was numerical and had leading spaces in order to align it to the right, you will need to adjust the field number. Squashing whitespace with tr -s ' '
first avoids having to deal with this.
To send that output into another file use redirection:
tr -s ' ' < columns.txt | cut -d" " -f2 > field2.txt
Using the awk command you could do something like the below which recognises automatically the field you are after because there is data there(?) I need to learn more about awk.
awk -F' ' '{print $2}' columns.txt