Good day, everyone!
I've got a small question to ask. If I'm getting the value for variable in following way:
LINE=$(grep "search word" myfile)
How can I access the n-th word on this line?
Thank you in advance for any explanations.
Good day, everyone!
I've got a small question to ask. If I'm getting the value for variable in following way:
LINE=$(grep "search word" myfile)
How can I access the n-th word on this line?
Thank you in advance for any explanations.
Apparently what you want is "awk":
LINE=$(awk '/search word/{print $3}' myfile)
So, for each line in "myfile" that matches the expression within / /, it will print the third field ($3) of that line. Each "field" is defined in awk as being separated by whitespace, although the separator can be user-defined.
n=3
LINE=$(grep "search word" myfile)
set -- $LINE
eval "nth_word=\${$n}"
Awk I thing I more suitable for those actions.
Thank a lot!