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)
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:
-
match all files ending with
.qcow2and then extract the full file name? Like:junos-vmx-x86-64-21.1R1.11.qcow2 -
match all files starting with
vFPCand then extract the full file name? Like:vFPC-20210211.img -
match all files starting with
vmxhddand 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.