Dallas Dallas - 4 months ago 12
Linux Question

How to extract particular string from a file (linux)

I have the following file

vol-12345678 gp2
vol-89dfg58g
VOLUMES 2016-03-17T22:03:08.374Z False 100 16 snap-7073d522 in-use vol-4568gds4 gp2
ATTACHMENTS 2016-03-17T22:03:08.000Z True /dev/sda1 i-181ed33c attached vol-7ea1c83f


etc.

etc.

I want to extract all instances of 'vol-********' and output it to a file (without the other contents) resulting in a file of:

vol-12345678

vol-34556767

vol-34534sdf

...

This is a relatively small file so I could do it manually, but I have another file with 200+ cases. Any idea how to use this using GREP or SED or AWK? Thanks!

agc agc
Answer

This should do it:

grep -o 'vol-[[:alnum:]]*' input.data > output.data