LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 08-25-2011, 06:39 AM   #1
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 129

Rep: Reputation: 20
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
vzdump-qemu-201-2011_08_19-18_09_13.tgz
vzdump-qemu-202-2011_08_19-18_11_45.tgz
vzdump-qemu-203-2011_08_19-18_14_05.tgz
vzdump-qemu-3321-2011_08_19-18_16_37.tgz
vzdump-qemu-7501-2011_08_19-18_18_32.tgz
vzdump-qemu-99000-2011_08_19-18_31_24.tgz
vzdump-qemu-99101-2011_08_19-17_58_40.tgz
vzdump-qemu-99201-2011_08_19-18_01_07.tgz
vzdump-qemu-99501-2011_08_19-18_02_28.tgz
to run the tool that i need; i require to know the ID of this file.

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,
 
Old 08-25-2011, 07:04 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hello nicolasdiogo,

I'd use Perl for this:
Code:
#!/usr/bin/perl

use strict;
use warnings;

opendir(TGZFILES, ".") or die "doesn't work $!\n";
my @file;
while (readdir TGZFILES) {
   if ($_ =~ m/\.tgz/) {
      @file = split "-", $_;
      print "id: ", $file[2], "   filename: ", $_, "\n";
   }
}
Markus
 
Old 08-25-2011, 07:39 AM   #3
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Ubuntu
Posts: 199

Rep: Reputation: 36
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)}'
 
Old 08-25-2011, 07:52 AM   #4
chickenjoy
Member
 
Registered: Apr 2007
Distribution: centos,rhel, solaris
Posts: 200

Rep: Reputation: 30
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)
 
Old 08-25-2011, 08:24 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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}'
If it is a script your after to also do other things, bash could also give it a whirl:
Code:
for file in vz*
do
    set -- ${file//-/ }
    echo "ID: $3"
done
 
Old 08-25-2011, 09:41 AM   #6
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Ubuntu
Posts: 199

Rep: Reputation: 36
Not only easier, it is much better your way grail.

Regards.
 
Old 08-26-2011, 02:34 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Code:
for file in vz*
do
    echo $file | cut -d'-' -f3
done
 
Old 08-26-2011, 06:03 AM   #8
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 129

Original Poster
Rep: Reputation: 20
thanks a lot Chris,

i finished the script using your suggestion many thanks
 
Old 08-28-2011, 09:19 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
No worries; glad I could help
 
  


Reply

Tags
bash, cut, script, split


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create iso images with long filenames bgraybr Linux - Software 7 07-01-2010 09:28 AM
Script to change weird filenames yeoheric Linux - Newbie 1 03-12-2008 05:41 AM
How to make XSane create individual filenames royeo Linux - Newbie 1 10-29-2006 12:05 AM
Script Question: Spliting A File tonyfreeman Programming 6 03-04-2006 04:35 AM
need help with script to remove all metachars from filenames BrianK Programming 5 08-21-2005 12:10 AM


All times are GMT -5. The time now is 03:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration