|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Search this Thread |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
doneRegards, Alister |
|
#5
|
|||
|
|||
|
Responses have come in quicker than I can login! Thank you all, problem is solved!
|
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |