adspace


How to get the nth word of a line in Unix?

Answer Posted / Ashish Mani Tiwari

In Unix, you can use a combination of `cut` and `tr` commands to extract the nth word from a given line. The basic idea is to count spaces (or any delimiter) up to `n-1`, then print the rest until the end of the line. Here's an example: `echo "This is a test" | cut -d" " -f$(($(echo "This is a test" | tr " "
| wc -l) - 2))` will give you the last word.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can you edit a large file without opening it in unix?

988