Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-12-2010
Registered User
 

Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Odd and even file names

Hello,

I want to sort/identify 600 files according to odd or even numbers in the files names. How can I do this?

The goal is to perform different ImageMagick operations based on even or odd numbers in the file names. The file names have this pattern: bdf0001.tif, bdf0044.tif and bdf0136.tif

I have all the ImageMagick code together but don't know how to recognise or sort the files according to odd or even file names. The sorting can be part of the ImageMagick script or I can alternatively separate these steps: first sort the files and move them to different directories. Then do the image processing part and put the files back together manually.

Any help or suggestions are greatly appreciated,
Gargan
Sponsored Links
    #2  
Old 03-12-2010
dennis.jacob's Avatar
Registered User
 

Join Date: Feb 2007
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 10 Times in 9 Posts
Are you looking for something like this ?


Code:
ls |  sed -n '/.*[02468]\.tif/p' | sort    # files with even numbers
ls |  sed -n '/.*[13579]\.tif/p' | sort    # files with odd numbers

Sponsored Links
    #3  
Old 03-12-2010
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,289
Thanks: 55
Thanked 427 Times in 408 Posts
An example how to move the file to the directories /dir/odd and /dir/even:

Code:
ls | awk '{s=$0;gsub(/[a-zA-Z]/,"",s);n=s%2?"odd":"even";print "mv "$0 " /dir/" n}'

If the output is correct you can pipe the output to sh to perform the action:

Code:
ls | awk '{s=$0;gsub(/[a-zA-Z]/,"",s);n=s%2?"odd":"even";print "mv "$0 " /dir/" n}' | sh

    #4  
Old 03-12-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
To relocate:

Code:
mkdir even odd; mv *[02468].tif even; mv *[13579].tif odd

For a list of each:

Code:
for f in *[02468].tif; do echo "$f"; done
for f in *[13579].tif; do echo "$f"; done

A test whose exit status can be used to decide on a further course of action (filenames without a digit are treated as if they contained a 0):

Code:
for f in *; do
    if [ "$(expr $(echo 0$f | tr -cd 0-9) % 2)" -eq 0 ]; then
        echo even
    else
        echo odd
    fi
done

Regards,
Alister
Sponsored Links
    #5  
Old 03-13-2010
Registered User
 

Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Responses have come in quicker than I can login! Thank you all, problem is solved!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Searching for file names in a directory while ignoring certain file names 2reperry Shell Programming and Scripting 2 12-13-2009 09:16 PM
Insert file names when concatenate files into a file samky2005 Shell Programming and Scripting 2 06-05-2009 06:07 PM
find for specific content in file in the directory and list only file names madhu_Jagarapu AIX 2 12-23-2008 01:13 AM
Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names nikosey Shell Programming and Scripting 6 07-30-2008 09:46 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 04:25 AM



All times are GMT -4. The time now is 03:42 AM.