LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-25-2005, 04:45 PM   #1
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Rep: Reputation: 15
group files by strings in dir


In a folder there are some files that have the first six characters of their names identical.
geis43*.txt
geis43*.xml
.
rayl18*.jpg
rayl18*.txt
rayl18*.xml
I need to apply some commands on each group of characters, like zipping the ones with a specific regex in their basenames, for instance, and dumping the others.
I've thought about declaring `ls .` as an array and declaring the basename of each file as another array. Then I'd loop an if test to compare each substring element of the arrays basename.
Not being any good in scripting, I suspect that there may be an easier way!
 
Old 10-25-2005, 04:52 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 56
What's wrong with something like: zip foo.zip geis43*

?
 
Old 10-25-2005, 05:08 PM   #3
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
The problem is that the directory contains hundreds of files and I may change them from time to time, even daily.
 
Old 12-16-2005, 12:40 PM   #4
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
${filename:0:5}

I suspect that the solution would be to chop the first 6 characters off declaring a variable with ${filename:0:5} and then a loop with chopped names:

Code:
do
       if [ -d ${i##*} ]
       then
           mv $i ${i##*}
       else
           mkdir ${i##*}
           mv $i ${i##*}
       fi
done
zip ${i##*}.zip ${i##*}*
rm `ls ${i##.*}* | grep -v ".zip"`
How do I define the variable containing the first 6 characters of the filename only?

Thanks.

Mel
 
Old 12-16-2005, 01:52 PM   #5
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
how about this...

Code:
#! /bin/bash

for base in `ls | sed "s/\(......\).*// | uniq`
do
   gzip $base* $base.zip
done
this will list all files in the working directory (if the script is in your $PATH variable, it will list all files in the directory you call it from), picks off the first 6 characters, then gets rid of any duplicate entries. so it will loop through all unique six character sequences and zip up all the files whose first six characters match into a file.
 
Old 12-17-2005, 09:37 AM   #6
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks, sirclif, but the code doesn't return anything.
I'm trying to correct the syntax.
Quote:
for base in .
do
togzip=`ls | sed -e 's/\(......\).*//' $base | uniq`
gzip $tozip* $tozip.zip
done
Probably I've made a number of mistakes up there.
 
Old 12-17-2005, 10:30 AM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
oops, i left something out of the sed command. try this...

Code:
#! /bin/bash

for base in `ls | sed "s/\(......\).*/\1/ | uniq`
do
   gzip $base* $base.zip
done
my bad.
 
Old 12-17-2005, 12:53 PM   #8
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
#! /bin/bash

for base in `ls | sed "s/\(......\).*/\1/" | uniq`
do
zip $base.zip $base*
done
did the trick for me.
Thank you very, very much
 
  


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 help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM
concatenating strings to open files veilig Programming 1 11-10-2003 05:36 PM
opening files with strings for file names veilig Programming 4 11-10-2003 11:19 AM
how to add dir to group Robin01 Linux - Newbie 2 09-09-2003 08:56 PM


All times are GMT -5. The time now is 12:10 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