I am using cut -d' ' -f1 to split a string in unix scripts. I want only f1 and f5, though, and I'm unable achieve this.

I tried giving -f1-5, but that prints all the fields from 1st through 5th.

cut -d' ' -f1,5

From man cut

-f, --fields=LIST
       select only these fields

Then later

Each LIST is made up of one range, or many ranges separated by commas.

awk can help:

echo "10 20 30 40 50" |awk '{print $1,$5}'


awk '{print $1,$5}' file.txt

You can even modify the results in place:

echo "10 20 30 40 50" |awk '{print $1*$5}' --> 500

(awk is cut on steroids)

  • To replicate the question options, you'd also need -F' '. – Sparhawk Nov 30 '18 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.