LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-07-2006, 12:09 PM   #1
varunnarang
LQ Newbie
 
Registered: Oct 2004
Location: India
Distribution: RHL 9.0
Posts: 22

Rep: Reputation: 15
Question Linux/Unix script for file pattern matching


Hello all,
In my project I have some entities and files corresponding to them. I need to copy each {Entity} file to a directory named {entity-dir}. A small example is:
<pre>
for entities: xxxxData_Itemxxxx.xml
xxxxData_ItemBomxxxx.xml
xxxxData_Procxxxx.xml
xxxxData_ProcDefxxxx.xml
to the directories
item
itembom
proc
procdef
</pre>

I was able to copy the files but there is a small problem with file-name pattern matching. When I search the pattern, let's say, for 'Item', it copies the file name xxxxData_Itemxxx.xml and xxxData_ItemBomxxxx.xml in the same directories, but I want them to go to different dirs.
Also the order is important,
(xxxx here is any characters that can appear before and after the entity name).
Please help me with pattern-matching.
Thanks & Regards,
Varun Narang
 
Old 08-07-2006, 01:14 PM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
This should work. I haven't tested, though, so try first with fake data and files:
Code:
for f in xxxxData_*xxxx.xml; do
  d=$(sed 's/^xxxxData_\(.*\)xxxx\.xml$/\1/' <<<"$f" | tr '[A-Z]' '[a-z]')
  [ ! -d "$d" ] && (mkdir "$d" || exit 1)
  mv "$f" "$d"
done
Yves.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM


All times are GMT -5. The time now is 02:04 AM.

Main Menu
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