Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using sed/awk to print ONLY words that contains matched pattern – Words starting with /pattern/ or Ending with /pattern/

I have the following output:

junos-vmx-x86-64-21.1R1.11.qcow2 metadata-usb-fpc0.img metadata-usb-fpc10.img 
metadata-usb-fpc11.img metadata-usb-fpc1.img metadata-usb-fpc2.img metadata-usb-fpc3.img 
metadata-usb-fpc4.img metadata-usb-fpc5.img metadata-usb-fpc6.img metadata-usb-fpc7.img 
metadata-usb-fpc8.img metadata-usb-fpc9.img metadata-usb-re0.img metadata-usb-re1.img 
metadata-usb-re.img metadata-usb-service-pic-10g.img metadata-usb-service-pic-2g.img 
metadata-usb-service-pic-4g.img vFPC-20210211.img vmxhdd.img

The output came from the following script:

images_fld=$(for i in $(ls "$DIRNAME_IMG"); do echo ${i%%/}; done)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The previous output is saved in a variable called images_fld=

Problem:

I need to extract the values of junos-vmx-x86-64-21.1R1.11.qcow2
vFPC-20210211.img and vmxhdd.img When I mean values I mean the entire word

The problem is that this directory containing all the files is always being updated and new files are added constantly which means that I can not rely on the line number ($N) to extract the name of those files.

I am trying to use awk or sed to achieve this

Is there a way to:

  1. match all files ending with.qcow2 and then extract the full file name? Like: junos-vmx-x86-64-21.1R1.11.qcow2

  2. match all files starting withvFPC and then extract the full file name? Like: vFPC-20210211.img

  3. match all files starting withvmxhdd and then extract the full file name? Like: vmxhdd.img

I am using those patterns as those files names tend to change names according to each version that I am deploying. But the patterns like: .qcow2 or vFPC or vmxhdd remain always the same regardless, so for that reason I need to extract the full string only by matching partial patterns. Is it possible? Thanks!

Note: I can not rely on files ending with .img as there are quite a lot of them so it would make it more difficult to extract the specific file names :/

>Solution :

This might work for you (GNU sed):

sed -nE '/\<\S+\.qcow2\>|\<(vFPC|vmxhdd)\S+\>/{s//\n&\n/;s/[^\n]*\n//;P;D}' file

If a string matches the required criteria, delimit it by newlines.

Delete up to and including the first newline.

Print/delete the first line and repeat.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading