Extract second word in line from text file

I currently need to extract the second word (CRISTOBAL) in a line in a text file.

* CRISTOBAL AL042014 08/05/14 12 UTC * 

The second word in the string "CRISTOBAL" will change day by day, so I just need to find a way to extract JUST the second word / character from the string.

+5
source share
2 answers

2nd word

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

will provide you CRISTOBAL

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

will give you only

You may need to change this if the line structure 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

 is CRISTOBAL 

2nd character

 echo 'testing only' | cut -c 2 

This will give you e , which is the 2nd character, and you may need to change it to suit your needs.

+10
source

If necessary sed / available only

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

Take the second non-empty group (assuming that after the word

there is at least 1 space)

But I prefer speed reduction (and awk if any manipulation is required).

In fact, it mostly depends on the next action in the script (if any).

+1
source

Source: https://habr.com/ru/post/1201090/


All Articles