Shell Script: 3 Methods to Find Out Nth Word in a String

advertisements

_____________________________________________________________________________________________________________________

The Eucharistic Miracles of the World
Here is my test string - This is a temporary change to complete the export. I wanted to find out nth word from the string.  I have specified the nth word in red color below
$ mystring="This is a temporary change to complete the export"

Method 1 – awk – 4th word

$ echo $mystring|awk '{ print $4}'
Temporary

Method 2 – Read – 5th word
$ read -ra sentence <<< "$mystring"; echo "${sentence[4]}"
Change

Method 3 – cut – 4th word
$ echo $mystring | cut -d" " -f4

temporary

_____________________________________________________________________________________________________________________

Website Stats

0 comments:

Post a Comment

Labels

Oracle (629) Script (86) General (77) Unix (47) Blog (23) Technology (19) gadget (6) games (6) Business (3) OCI (3) SQL* Loader (3) Datapump (2)
 

acehints.com Copyright 2011-23 All Rights Reserved | Site Map | Contact | Disclaimer