Odd and even file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Odd and even file names
# 1  
Old 03-12-2010
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
# 2  
Old 03-12-2010
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  
Old 03-12-2010
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
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
# 5  
Old 03-13-2010
Responses have come in quicker than I can login! Thank you all, problem is solved!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seperate Odd and Even numbers from 1 file to 2 files

Hey guys. I have been trying to figure out an easy way to seperate a liste of 150k numbers (10 digits) in a .txt file into odd and even numbers with each of their own files, for a project at work. I've tried Excel, but it was too much for it and it wasnt very simple. So i gave up after... (13 Replies)
Discussion started by: TranceC
13 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

4. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

5. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

6. Shell Programming and Scripting

odd problem in read lines from file

Hi, I wrote a small program to read lines from a file and count the lines. The program is as below: filename=$1 count=0 cat $filename | while read -r line do printf "%5d:%s\n" $count "$line" count=$((count + 1)) done echo " $count " After I run the program, the result is... (4 Replies)
Discussion started by: jianma
4 Replies

7. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

8. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

9. UNIX for Dummies Questions & Answers

Odd File Listing and unable to deleted

Hi, I'm trying to delete some files that are causing a script to malfunction. I cannot seem to remove them even with -f. I have tried chmod and chown and they don't seem to be affected the files at all. they have weird dates listings, too. Here is their listing: br-xr-xrwt 29561 538995051... (3 Replies)
Discussion started by: Jason Brice
3 Replies

10. UNIX for Dummies Questions & Answers

Odd file with no name

OS: Solaris 2.6 File with no name created Mar of 2000 - ls (with or without options) shows the file but no name associated with it. Example: ls -ltca -rw-r--r-- 1 root other 9721 Apr 16 2003 printcap -rw-r--r-- 1 root other 267 Apr 16 2003 -rw-r--r-- 1 root other 258 Apr 16... (3 Replies)
Discussion started by: RTM
3 Replies
Login or Register to Ask a Question