Register
It is currently Fri Apr 18, 2014 12:12 am

Script to extract parts of a filename


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Jul 14, 2009 6:38 am   

Joined: Fri Mar 06, 2009 8:42 am
Posts: 5
Hi guys,

Im trying to make a script that extracts a string from a filename. I tried using "cut", "awk" and few more things but with no success.

The filename has several parts separated by "_", something like "date_name1_name2_destination.txt" and I need to get the "name1" and "name2" parts, and create subdirectories named "name1" and "name2"

Any help is welcome, Im sure this is actually pretty easy to make.


Top
 Profile  
 PostPosted: Tue Jul 14, 2009 8:33 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Is the filenames always in the format of date_name_name2_destination.txt?

in that case you can use cut with ease :)
Code:
# The filename which is going to be chopped
file="20090714_name1_name2_adest.txt"
# "name1"
echo $file | cut -d'_' -f2
# "name2"
echo $file | cut -d'_' -f3


if you want these in variables, then you can do like this:
Code:
name1=$(echo $file | cut -d'_' -f2)
name2=$(echo $file | cut -d'_' -f3)


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Jul 15, 2009 1:01 am   

Joined: Fri Mar 06, 2009 8:42 am
Posts: 5
Thanks a lot man, thats all I needed :]
I actually tried something like that at first, but it seems I have messed up the delimiter part.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP