Register
It is currently Fri Dec 19, 2014 4:23 am

grep odd number in file name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sat Nov 07, 2009 5:13 pm   

Joined: Sat Nov 07, 2009 5:00 pm
Posts: 7
I have scan several file and want to apply the imagemagick convert -rotate command , with a script , but only on file with a odd number .

Each name file contain a number from 1 to 24 , when I do an ls on the directory it doesn't seem to respect the normal number sequence (1,2,3,4,5,...) , here is what it give me :

Code:
gnu@robby:~/Documents/tp/Documents$ ls | grep dotrine.dr.etranger.tp- | nl
     1   dotrine.dr.etranger.tp-10.jpg
     2   dotrine.dr.etranger.tp-11.jpg
     3   dotrine.dr.etranger.tp-12.jpg
     4   dotrine.dr.etranger.tp-13.jpg
     5   dotrine.dr.etranger.tp-14.jpg
     6   dotrine.dr.etranger.tp-15.jpg
     7   dotrine.dr.etranger.tp-16.jpg
     8   dotrine.dr.etranger.tp-17.jpg
     9   dotrine.dr.etranger.tp-18.jpg
    10   dotrine.dr.etranger.tp-19.jpg
    11   dotrine.dr.etranger.tp-1.jpg
    12   dotrine.dr.etranger.tp-20.jpg
    13   dotrine.dr.etranger.tp-21.jpg
    14   dotrine.dr.etranger.tp-22.jpg
    15   dotrine.dr.etranger.tp-23.jpg
    16   dotrine.dr.etranger.tp-24.jpg
    17   dotrine.dr.etranger.tp-2.jpg
    18   dotrine.dr.etranger.tp-3.jpg
    19   dotrine.dr.etranger.tp-4.jpg
    20   dotrine.dr.etranger.tp-5.jpg
    21   dotrine.dr.etranger.tp-6.jpg
    22   dotrine.dr.etranger.tp-7.jpg
    23   dotrine.dr.etranger.tp-8.jpg
    24   dotrine.dr.etranger.tp-9.jpg


It seems ls classify the file according a kind of version number like 1.0 , 1.1 , 1.3 instead of 1,2,3.... 13,14,15 ......

About the script , I thought something like this

Code:
#!/bin/bash

for i in `ls | grep dotrine.dr.etranger.tp- | nl `
do convert -rotate 90
done


but how can grep the odd number in file name ?
Also can someone explain me why the normal number sequence isn't respected ? (it 's not the first time i saw that )

thank you :-t


Top
 Profile  
 PostPosted: Sun Nov 08, 2009 8:27 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
First of all, it's never a good idea to do
Code:
for var in $(ls | grep | whatever; do ... ; done
prefer
Code:
for var in /path/dir/filename*
if 'filename' can be a pattern, or
Code:
for var in /path/dir/*
wich will return any file (including directories) in /path/dir.
In your case:
Code:
for file in dotrine.dr.etranger.tp-{1..24}.jpg; do
convert -rotate 90
done
should do the trick.


Top
 Profile  
 PostPosted: Mon Nov 09, 2009 2:14 pm   

Joined: Sat Nov 07, 2009 5:00 pm
Posts: 7
Thank you for your hint , the code seems to execute the rotation on every file from 1 to 24 . How can I grep only the odd number file name ?
Code:
    for file in dotrine.dr.etranger.tp-{1..24}.jpg; do
    convert -rotate 90
    done


Top
 Profile  
 PostPosted: Mon Nov 09, 2009 4:49 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
Code:
for file in dotrine.dr.etranger.tp-{1..24}.jpg
do
   subfile=${file#*-} filenb=${subfile%.*}
   [ $(( filenb % 2 )) = 1 ] && echo "convert -rotate 90 $file"
done
convert -rotate 90 dotrine.dr.etranger.tp-1.jpg
convert -rotate 90 dotrine.dr.etranger.tp-3.jpg
convert -rotate 90 dotrine.dr.etranger.tp-5.jpg
convert -rotate 90 dotrine.dr.etranger.tp-7.jpg
convert -rotate 90 dotrine.dr.etranger.tp-9.jpg
convert -rotate 90 dotrine.dr.etranger.tp-11.jpg
convert -rotate 90 dotrine.dr.etranger.tp-13.jpg
convert -rotate 90 dotrine.dr.etranger.tp-15.jpg
convert -rotate 90 dotrine.dr.etranger.tp-17.jpg
convert -rotate 90 dotrine.dr.etranger.tp-19.jpg
convert -rotate 90 dotrine.dr.etranger.tp-21.jpg
convert -rotate 90 dotrine.dr.etranger.tp-23.jpg
I put an `echo` 'cos I don't have files to test.

This tests the modulo of the number in the file name: odd number modulo 2 equals 1.


Top
 Profile  
 PostPosted: Tue Nov 10, 2009 12:38 am   

Joined: Sat Nov 07, 2009 5:00 pm
Posts: 7
Thanks! I just don't understand how did you grab the number in file name ?


Top
 Profile  
 PostPosted: Tue Nov 10, 2009 2:31 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
eastwind wrote:
Thanks! I just don't understand how did you grab the number in file name ?


The ${file#*-} removes everything before the "-". which means he's left with {1..24}.jpg, ${file%.*} looks for the "." and removes everything after it.
Atleast I believe that's how it works ;P I've seen magical things done like this in bash many times, just seem to forget how the work way too often :P

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Nov 10, 2009 5:49 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
that's it Fredrik.
more details in
Code:
man -LC -P'less -p"Parameter Expansion"' bash


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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