Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page [SOLVED] An interesting scripting problem involving Playboy's Playmates!!!
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Closed Thread
 
Thread Tools Display Modes
Old 06-16-2008   #1 (permalink)
Just Joined!
 
Join Date: Jun 2008
Posts: 3
Wink [SOLVED] An interesting scripting problem involving Playboy's Playmates!!!

Hello all!

A few days ago I was doing a Google image search for something (don't remember what) and as it happens often, one of the results was a totally irrelevant picture of a very beautiful naked lady.

So naturally I click at the image and it turns out it's a picture of a Playboy Playmate taken long before I was born. So I go one step back in this web site and it turns out this guy has pictures of ALL the playmates taken from 1953 (Marilyn Monroe was the first) to 2007. The pictures are the poster that comes with every playboy issue and are of high quality, categorized by decade.

Well now I'm curious and I want to download them all so I find a program that downloads whole websites (or website directories) and I proceed to download/ install the program and feed it the home page of this strange site.

However the download is slow (12kb/s) and I'm beginning to get bored (or impatient...). So I do a torrent search and I discover that someone has
posted a zip file that contains just what I'm looking for...

As luck would have it the torrent download is much faster and so both things finish at just about the same time. After reviewing the two sets of pictures I got I realize that while the picture dimensions and file sizes are almost the same for the two sets I downloaded, the images in the torrent zip file are better. The tone is better contrast/saturation/colors etc. So I decide to keep the torrent file and delete the one's I got from the guy's web site.
Now the pictures from the website have user friendly names such as:

7203_Ellen_Michaels.jpg

So you know that playmate of the Month in March 1972 was Ellen Michaels
(and she was very beautiful indeed, take my word for it). However the pictures
from the torrent that are better looking have stupid names such as:

centerfold-PM197203A1-01-lrg.jpg

AND HERE LIES THE PROBLEM... I want to rename the better looking pictures using the name from the other collection... Of course you could do it by hand but that is both ridiculous and time consuming. The correct solution of course is to write a unix script that does just that in no more than 10 lines.

Below is the pseudo code for my (ingenious) script that has yet to be written:

1) Do a listing of directory one (web site) and save the contents in array1
2) Do a listing of directory two (torrent) and save the contents in array2
3) for (i=0; i <array1 elements; i++ )
rename /home/user/directory/array2[i] to /home/user/directory/array1[i]

However I'm not very familiar with array manipulations in a script
so if you are still reading this (and I doubt it) here come the questions:

1) How would you write my script?

2) Is there a more efficient way to do this job? List different ways to attack this problem. Would you use awk or sed?

3) Can you do this job not in a unix script but in a single line using pipes?

Thanks and have fun!
foufoutos is offline  
Old 06-19-2008   #2 (permalink)
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 1,017
Quote:
1) How would you write my script?

2) Is there a more efficient way to do this job? List different ways to attack this problem. Would you use awk or sed?

3) Can you do this job not in a unix script but in a single line using pipes?
[The sound of crickets can be heard across the meadow.]
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is offline  
Old 06-19-2008   #3 (permalink)
Linux Enthusiast
 
Freston's Avatar
 
Join Date: Mar 2007
Posts: 573
That's ... erm ... interesting

You are looking for three values. #1 year #2 month #3 name. And then compare the first two between the two 'data sets' to append value 3 taken from set 1 to the corresponding filename in set 2.

Your life will be easier if you have all images from set 1 in one dir, and all images from set 2 in another dir.

I would use neither awk or sed, but parameter substitution.

If you divide names like this up in separate variables:
./set1/7203_Ellen_Michaels.jpg

$YEAR = 72 #first two digits
$MONTH = 03 #next two digits
$NAME = _Ellen_Michaels.jpg #everything starting from first '_'

then copy (the safe choice) ./set2/centerfold-PM19$YEAR$MONTH*
./anotherdir/$YEAR$MONTH$NAME

Although it is possible to do this all in a single input line in the shell, I don't recommend it. Because it'll involve a 'for loop', and a lot of temporary variables. Also, I don't know if filenames are absolutely consistent, usually there are slight variations invisible to the human eye but wreaking havoc on the behavior of your script.
__________________
Can't tell an OS by it's GUI
Freston is offline  
Old 06-20-2008   #4 (permalink)
Just Joined!
 
Join Date: Jun 2008
Posts: 3
thanks for replying Freston but you are not really solving my problem.
your method seems more complicated than mine.

Can you at least tell me how I can do an ls and save the contents (the file names) in an array?

Thanks
foufoutos is offline  
Old 06-20-2008   #5 (permalink)
Linux Enthusiast
 
elija's Avatar
 
Join Date: Jul 2004
Location: This week I am mostly using Workbench
Posts: 568
I don't think you can do your way... You can't say with any
certainty that the arrays will be in the same sequence.

Personally, I would use Perl and build the new file name by pulling
the relavent bits from the old one and then copying the file to a
new directory.

It could probably be done in bash scripting, but I wouldn't know how
__________________
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
(How much wood would a woodchuck chuck if a woodchuck could chuck wood)

Registered Linux User: #459086

PM is not a good way to get help. Please ask in the forums.
elija is offline  
Old 06-20-2008   #6 (permalink)
Linux Enthusiast
 
Freston's Avatar
 
Join Date: Mar 2007
Posts: 573
Quote:
Originally Posted by foufoutos
thanks for replying Freston but you are not really solving my problem.
your method seems more complicated than mine.
Well, I'm not trying to solve your problem. I just pointed out the option of parameter substitution as a probable means to solve your problem. I'm sorry if I made it sound complicated. It is how I would approach this matter.

Quote:
Originally Posted by foutoutos
Can you at least tell me how I can do an ls and save the contents (the file names) in an array?
I have no idea why you'd want to do that. It's an extra step and unnecessary in my opinion. Also I have no way of knowing if the arrays will be in the same order.

Let's say you have these directories:
~/test
~/test/torrentpics
~/test/websitepics
~/test/results

Now you run: (Untested!!!)
Code:
for i in ~/test/websitepics/* ; do
   yearmonth=${i:0:4}
   name=${i#*_}
   ls ~/test/torrentpics/centerfold-PM19$yearmonth* && echo is $name || echo $i doesn't match
done
See if this gives errors.

What this does is compare all the year&month fields in the website pics to the year&month field in the torrent pics. It'll echo the name field from the website pics below the corresponding torrent pics, thus allowing to see if there are duplicates.
Also, when no match is found, it'll echo the website pic name corresponding to the missing match.

If this works, then you can cp the torrent files to their new location using the yearmonth and name variables you just created to create the new names.


But how to do that depends on how consistent the torrent files are in their naming scheme. If there are inconsistencies or duplicates with slightly different names, then you'll need to iron that out first. You can use the above script to find if such is the case.


EDIT: In retrospect, is this understandable what I wrote? 'cuz I'd hate it if I sound like one of those posters you hear about, the one with an attitude. I'm only learning these bash things myself. And I know sometimes there is a little trick behind a mechanism that makes life easier. Parameter substitution is just the tool for the job IMHO. But the rest of the snippets of scripts I wrote are quick and dirty. Again, I'm not offering a cheap solution, I'm just trying to help you by offering a mechanism that you can use to write something yourself. With some close examples that wont destroy your system.

It's also not the only solution in town. You can sed or grep or awk your way through this. And you can build arrays with output redirection. That might be as easy as:
ls . > array
or as elaborate as giving each file a variable name with an incremental number appended to takings out of it's filename. But as I said, that is an extra step. You only need that if you want to work with items of datasets numbers of times, for example if you want your script to be interactive; you can have it flag mismatches and ask for human input on what to do with them (Abort, Ignore, Fail?) _ (<=blinking prompt)

PS. Welcome to the forums!
__________________
Can't tell an OS by it's GUI
Freston is offline  
Old 06-21-2008   #7 (permalink)
Just Joined!
 
Join Date: Jun 2008
Posts: 3
Finally I did it!

Hi

I finally did it, by sticking into my original logic of:
rename this into that.
I don't see a reason to get dirty with #years, #months etc.

Each set of files has six directories representing decades. There are no inconsistencies or duplicates.

I redirected the output into a log file to save time and the whole process of renaming 649 files took 2.92 seconds in a vmware FreeBSD system. That's not bad! Of course if I had written a java program to do the same thing it would have taken me about 5 minutes instead of days (then again that program would have been slower and I wouldn't have learned anything new about scripting!)

The output in the log file looks like this:

Starting process for 50s
Same number of files in the two directories
Renaming centerfold-PM195312A1-01-lrg.jpg into 5312_Marilyn_Monroe.jpg
Renaming centerfold-PM195401A1-01-lrg.jpg into 5401_Margie_Harrison.jpg
Renaming centerfold-PM195402A1-01-lrg.jpg into 5402_Margaret_Scott.jpg
Renaming centerfold-PM195403A1-01-lrg.jpg into 5403_Dolores_Del_Monte.jpg
Renaming centerfold-PM195404A1-01-lrg.jpg into 5404_Marilyn_Waltz.jpg
...


the 'juice' of the program is just 3 lines:

make 2 arrays with the names:

Code:
web_files=(`ls $HOME_DIR/$WEB_DIR/${web_decade[i]}`)
torrent_files=(`ls $HOME_DIR/$TORRENT_DIR/${torrent_decade[i]}`)
and use it to rename the files:

Code:
`mv  $HOME_DIR/$WEB_DIR/${web_decade[i]}/${web_files[x]} $HOME_DIR/$WEB_DIR/${web_decade[i]}/${torrent_files[x]}`
Below is the complete code for anyone interersted
(don't you have any sex life at all? ) :

Code:
#!/usr/local/bin/bash

HOME_DIR="/home/some_user"
TORRENT_DIR="playboy/torrent"
WEB_DIR="playboy/web"

torrent_decade=( 50s 60s 70s 80s 90s 00s )
web_decade=( "Dec53-Dec59" "Jan60-Dec69" "Jan70-Dec79" "Jan80-Dec89" "Jan90-Dec99" "Jan00-Dec09")

total_num_of_files=0

echo "" > ~/renamer.log

for  i in 0 1 2 3 4 5   
   do
      # get list of files
      web_files=(`ls $HOME_DIR/$WEB_DIR/${web_decade[i]}`)
      torrent_files=(`ls $HOME_DIR/$TORRENT_DIR/${torrent_decade[i]}`)
     
     #save original names in a backup file
     echo "`ls $HOME_DIR/$WEB_DIR/${web_decade[i]}`" > ${web_decade[i]}.backup
      
     echo "Starting process for ${torrent_decade[i]} " >> ~/renamer.log
      
     if [  ${#web_files[*]} != ${#torrent_files[*]} ]
       then
         echo "Error! Number of files in the two directories don't match! Exiting"
	 exit 0;
       else
         echo "Same number of files in the two directories" >> ~/renamer.log
         let "total_num_of_files+=${#web_files[*]}" #keep score...
     fi
     
     # rename the files
     for(( x=0; x < ${#torrent_files[*]} ; x+=1 ))
     do         
        echo  "Renaming ${web_files[x]} into ${torrent_files[x]}" >> ~/renamer.log
	`mv  $HOME_DIR/$WEB_DIR/${web_decade[i]}/${web_files[x]} $HOME_DIR/$WEB_DIR/${web_decade[i]}/${torrent_files[x]}`
     done
            
  done
  
  echo "Total Number of files renamed: $total_num_of_files"

exit 0;

Thanks for your time guys!
foufoutos is offline  
Closed Thread



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 07:10 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0