0

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 and 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 and grep commands but It could extract the number.

2

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
0

we can try with below command , Tested and worked fine

awk '{for(i=1;i<=NF;i++){if($i ~ /^GN-[0-9]*$/){print $i}}}' filename

output

GN-4536
GN-497187

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy