Extract The Second Word In A Line From A Text File Extract The Second Word In A Line From A Text File unix unix

Extract The Second Word In A Line From A Text File


2nd word

echo '*   CRISTOBAL  AL042014  08/05/14  12  UTC   *' | awk  '{print $2}'

will give you CRISTOBAL

echo 'testing only' | awk  '{print $2}'

will give you only

You may have to modify this if the structure of the line changes.

2nd word from lines in a text file

If your text file contains the following two sentences

this is a test*   CRISTOBAL  AL042014  08/05/14  12  UTC   *

running awk '{print $2}' filename.txt will return

isCRISTOBAL

2nd character

echo 'testing only' | cut -c 2

This will give you e, which is the 2nd character, and you may have to modify this so suit your needs also.


In case of sed is needed /only available

sed '^ *[^ ]* *\([^ ]*\) .*/\1/' YourFile

Take second non blank group (assuming there is at least 1 blank after the word

But I prefer cut for speed (and awk if any manip is needed).

In fact it mainly depend on next action in script (if any).


I know it is old but, @NeronLeVelu was hair short of what I needed in a pipe.

ip link show wlan0 | grep 'link'| sed 's/^ *[^ ]* *\([^ ]*\) .*/\1/'

Was what I used to extract the MAC address. I could not use awk for other reasons.