Extract a word with specific prefix

Am3Y asked:

Hello I have a file with lines such as

27/12/20 | 05:50:48pm | abcdefgh  |ID:1:418856791 | jack | 1.1.1.1:1111 | IN | GN-4536 | LOL | 0
27/12/20 | 05:51:44pm | abcdefgh  |ID:1:503729784 | jill | 2.2.2.2:2222 | IN | GN-497187 | LOL2 | 0

There is a big file with such lines. How can I extract only GN-4536 & GN-497187 from this file? Like it can be anything GN-numbers. How can I get the output in format like

GN-number
GN-number
GN-number
GN-number

I tried using sed & grep commands but It could extract the number. I used to only get the result as GN.

Someone plzz help me

My answer:


This is pretty easy with cut:

$ cut -f8 -d'|' filename
 GN-4536 
 GN-497187 

This cuts the 8th field delimited by | (which must be escaped from the shell).

But it leaves in the whitespace. You can strip that with tr.

$ cut -f8 -d'|' filename | tr -d ' '
GN-4536
GN-497187

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.