Warm tip: This article is reproduced from serverfault.com, please click

extract and print all occurrences of disk file (.img) from a configuration file

发布于 2020-12-01 19:43:53

I have vm configuration files from which I need to print all the disks (26 alphanumeric characters followed by .img) existing within each file.

here is an extract of one of the files

[root@~]# cat demo_vm.cfg
disk = ['file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb0000120000a17dfe12ac74818f.img,xvda,w', 'file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb0000120000e66ace31dac64d98.img,xvdb,w', 'file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb000012000082fbb45a02e24096.img,xvdd,w']

I want to extract the below (all references of 26alphanum.img in the file) :

0004fb0000120000a17dfe12ac74818f.img
0004fb0000120000e66ace31dac64d98.img
0004fb000012000082fbb45a02e24096.img

some files have 3 disks some have only one for which I usually run this and have what I want but in case of multiple occurrences I can only print the first one.

# awk -F [/,] '/disk/ { print $6}' demo_vm.cfg
0004fb0000120000a17dfe12ac74818f.img

Thanks in advance I spent hours trying splits and regex patterns without conclusive result. This is my first question in SOverflow.

EDIT

here are the 3 types of content put in separate files (1= one 26[alnum].img occurrence, 2= two 26[alnum].img occurrences , 3= three 26[alnum].img occurrences )

# cat demo_vm_1.cfg
disk = ['file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb000012000065a82a4df5e7112b.img,xvda,w']
[root ~]# cat demo_vm_2.cfg
disk = ['file:/OVS/Repositories/0004fb0000030000a079ca25909e5455/VirtualDisks/0004fb0000120000822cb8b0602ee042.img,xvda,w', 'file:/OVS/Repositories/0004fb0000030000a079ca25909e5455/VirtualDisks/0004fb000012000073d5fd864a0ba6b1.img,xvdb,w']
# cat demo_vm_3.cfg
disk = ['file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb0000120000a17dfe12ac74818f.img,xvda,w', 'file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb0000120000e66ace31dac64d98.img,xvdb,w', 'file:/OVS/Repositories/0004fb00000300007b8afb76a3377693/VirtualDisks/0004fb000012000082fbb45a02e24096.img,xvdd,w']
  • Initial script

my initial script that creates the remove commands for the .cfg files and the pointed images inside each of them had a problem when the cfg had more than one disk reference. I guess I can adapt it now to use grep -Eo instead of awk

  
strings=(`find  /vm_backup/VirtualMachines/*/vm.cfg`)                                                             
for i in "${strings[@]}"; do                                                                                      
echo "rm -f $i" >> drop_vm_final.sh                                                                               
awk -F [/,] '/disk/ { print $6}' "$i" | awk '{print "rm -f  /vm_backup/VirtualDisks/"$0}'  >>drop_vm_bkp_final.sh 
done                                                                                                              

Questioner
koss
Viewed
0
Ed Morton 2020-12-02 03:50:42
$ grep -Eo '[[:alnum:]]{26}\.img' file
0000120000a17dfe12ac74818f.img
0000120000e66ace31dac64d98.img
000012000082fbb45a02e24096.img

If that's not all you need then edit your question to provide more truly representative sample input/output that that doesn't work for.