0

I am using an old (7.3) version of FreeBSD, so I can't use length, substr, index, or match arguments of expr command. I want to extract the fourth word from the command's output (without a script). I actually can do this by using backticks, or a pipe, but I'll have to use a script, which only contains echo $4. That's kind of awkward. Which command would you suggest to use to parse an input string from another command? I heard of awk and sed, but I'm unsure if they are those which I need.

1
  • what version of bash do you have? Sep 11, 2014 at 10:20

1 Answer 1

1

You should just be able to use:

fourth_word=$(your_command | awk '{print $4}')

as in the following transcript:

pax> fourth_word=$(echo ab cd ef gh ij | awk '{print $4}')
pax> echo $fourth_word
gh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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