create script by spliting filenames
hello (again!)
folks i am trying to write a bash script to restore some files from a bunch of archives. the archive are named as follows: Code:
ls for the first file (vzdump-qemu-201-2011_08_19-18_09_13.tgz) this ID is 201 so i need to split/cut out the filename and get this ID for each file. i have tried to find the solution - but i have not found many articles that described what they were doing - and instead i found a lot of pieces of bash coding that did something that i did not understand. many thanks, |
Hello nicolasdiogo,
I'd use Perl for this: Code:
#!/usr/bin/perl |
For me awk is the God, so here is my solution (acording to the name format you have provided).
ls vz*|awk '{print substr($1,13,length($1)-36)}' |
just to help clarify for everyone: is the ID always the first 3 digits? or the numbers in between the 2nd dash and the 3rd dash? (ex: 99501 for the last file)
|
I think it depends what else you might need to do, but if you were to use ls and awk as suggested by rodrifra, I think this would be easier:
Code:
ls vz* | awk -F- '{print "ID:",$3}' Code:
for file in vz* |
Not only easier, it is much better your way grail.
Regards. |
Code:
for file in vz* |
thanks a lot Chris,
i finished the script using your suggestion many thanks |
No worries; glad I could help :)
|
All times are GMT -5. The time now is 11:20 AM. |