How to Get the Nth Word of a Line in Unix

Embed Size (px)

DESCRIPTION

zc

Citation preview

  • How to get the nth word of a line in Unix?Assuming the words in the line are separated by space, we can use the [cut] command. [cut] is a very powerful and useful command and it's real easy. All you have to do to get the n-th word from the line is issue the following command:

    cut f -d' ''-d' switch tells [cut] about what is the delimiter (or separator) in the file, which is space ' ' in this case. If the separator was comma, we could have written -d',' then. So, suppose I want find the 4th word from the below string: A quick brown fox jumped over the lazy cat, we will do something like this:$> echo A quick brown fox jumped over the lazy cat | cut f4 d' 'And it will print fox