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



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

Answer / 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

More Unix General Interview Questions

command to show the space allocation of files?

3 Answers   HP,


What do you mean by the protection fault?

1 Answers  


what is wrong with this interactive shell script?

1 Answers  


How do I find my unix os version?

1 Answers  


Explain how and when a DNS request is solved when a user tries to surf to the site www.abcdeg.in .Assume the user machine uses a local name server and sits on another network other than www.abcdef.in.

2 Answers  


What is grep|sort give example?

1 Answers  


What does $? Mean in unix?

1 Answers  


What is umask in unix?

1 Answers  


What is difference between ssh v1 and ssh v2?

1 Answers  


following two file are file a file b 110 aab 330 xxl i want the output like file a fileb 110 xxl 330 aab give solution ?

3 Answers   Accenture,


How to perform a system shutdown in unix?

1 Answers  


What is inode in unix operating system?

1 Answers  


Categories