Find the answer to your Linux question:
Results 1 to 7 of 7
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 ...
  1. #1
    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!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  3. #3
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    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

  4. #4
    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

  5. #5
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    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
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  6. #6
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    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

  7. #7
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...