Sponsored Content
Top Forums Shell Programming and Scripting Perl: sorting files by whats in'em Post 41703 by quantumechanix on Sunday 12th of October 2003 06:22:22 PM
Old 10-12-2003
Perl: sorting files by whats in'em

i'm halfway into my script and realize i may need to use an associative array (a hash, i guess they are called in Perl). i'm fairly new to Perl and don't know how to use them correctly.

i have some files in a directory. each file contains a number in a specific place in it's header. what i would like to do is create a list of the files in a text file that has them sorted according to this number. there is also one other thing. some of the files' numbers "overlap" and i don't want these files included. since this is kind of weird to explain i'll use the following example:

below is the list of files along with their associated numbers before they are sorted. notice at "file12" the numbers start overlapping and starting with "file21" the numbers reach the point past -0.5 (which is file11's number). i basically do not want files 12 through 20 included in my text file.

"file1",-30.5
"file2",-27.5
"file3",-24.5
"file4",-21.5
"file5",-18.5
"file6",-15.5
"file7",-12.5
"file8",-9.5
"file9",-6.5
"file10",-3.5
"file11",-0.5
"file12",-26.5
"file13",-23.5
"file14",-20.5
"file15",-17.5
"file16",-14.5
"file17",-11.5
"file18",-8.5
"file19",-5.5
"file20",-2.5
"file21",0.5
"file22",3.5
"file23",6.5
"file24",9.5
"file25",12.5
"file26",15.5
"file27",18.5
"file28",21.5
"file29",24.5
"file30",27.5
"file31",30.5

here is the way the text file with the sorting should look:

"file1",-30.5
"file2",-27.5
"file3",-24.5
"file4",-21.5
"file5",-18.5
"file6",-15.5
"file7",-12.5
"file8",-9.5
"file9",-6.5
"file10",-3.5
"file11",-0.5
"file21",0.5
"file22",3.5
"file23",6.5
"file24",9.5
"file25",12.5
"file26",15.5
"file27",18.5
"file28",21.5
"file29",24.5
"file30",27.5
"file31",30.5

Note: the quotes aren't part of the file names and the file names for the files are not named so that you can "deduce" the order (file1 -> file31), they are for all intents and purposes random. Also, i know the numbers increase by 3 (and that is how they increase), but the gap in between the cut off is only 1 (from -0.5 to 0.5), and this is ok. i basically cannot have a gap more than 3.

what i'm thinking is that an associative array may be appropriate, but i'm not sure. my logic so far has been to first, find the smallest number and write it's corresponding filename to the text file. then find the filename with the first file's number + 3, and append that filename to the textfile. then, if no more files fit that criteria, start appending the filenames by which is larger than the last one. i think this would work but i don't know how to do it.

thanks in advance for helping. this will save me lots of time, since i have many directories like this and sorting them by hand takes a long time.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

renaming 50k files, whats the best way?

Because I am not creative, I did this: find . -type f -name '*.GIF'|cut -d'/' -f2|awk -F. '{print "mv "$1".GIF "$1".gif --reply=yes"}' > case.sh Then ran the case.sh - I was wondering if you guys could come up with something more efficient? Or even limit CPU useage? It is killing my poor ext3... (3 Replies)
Discussion started by: r0sc0
3 Replies

2. Shell Programming and Scripting

perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following: Joe_________12_Main_St__A001________LX Benny_______5_Spring____A002________LX... (5 Replies)
Discussion started by: joeyg
5 Replies

3. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

4. Shell Programming and Scripting

whats wrong with this line using perl

E:\>perl -00ne 'push @a,"$_\0$ARGV\n";END{print reverse split/\0/ for sort @a}' file1-obj_prof.out.txt file2-obj_prof.out.txt' Can't find string terminator "'" anywhere before EOF at -e line 1. (6 Replies)
Discussion started by: richsark
6 Replies

5. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

6. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

7. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

8. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

10. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies
fatsort(1)						      General Commands Manual							fatsort(1)

NAME
FATSort - FAT filesystem sorting utility SYNOPSIS
fatsort [options] device DESCRIPTION
FATsort sorts directory structures of FAT filesystems. Many MP3 hardware players don't sort files automatically but play them in the order they were transmitted to the device. FATSort can help here. OPTIONS
-c This option causes FATSort to ignore the case of filenames (e.g. 'example.mp3' is equal to 'EXAMPLE.MP3'). -f Force sorting even if the device is mounted or if FATSort cannot determine if the device is mounted. -h Shows some help information. -i Prints some file system information of the specified device. -l FATSort will not sort directory structures, but just print the current order. -o type This options specifies how FATSort will sort files and directories. type can be 'd' for directories before files (which is the default), 'f' for files before directories, or 'a' for no differentiation between files and directories. -n Uses natural order to sort directory structures instead of alphanumeric order. For example, an alphanumeric order would be file123.mp3 file21.mp3 file3.mp3 and the corresponding natural order would be file3.mp3 file21.mp3 file123.mp3. -q FATSort will work quietly without printing additional information messages. -r Sorts the directories in reverse order (Z-A). -R Sorts the directories in random order. -v Shows version information only. The following options can be specified multiple times: -d directory Sort directory only -D directory Sort directory and all subdirectories -x directory Don't sort directory -X directory Don't sort directory and all subdirectories -I prefix Ignore filename prefix prefix during sorting. For example, passing option -I "the " tells FATSort to sort 'The Beatles.mp3' like 'Beatles.mp3'. EXAMPLES
Sort FAT filesystem on an USB stick: fatsort /dev/sda1 Sort FAT filesystem in a file: fatsort /home/user/fat16_fs.img Sort directory /dir1 and all subdirectories except /dir1/dirA: fatsort /dev/sda1 -D /dir1 -x /dir1/dirA Ignore prefixes 'a ' and 'the ' during sorting: fatsort /dev/sda1 -I "a " -I "the " AUTHOR
Boris Leidner <fatsort(at)formenos.de> SEE ALSO
mount(8) fsck(8) NOTES
FAT12 is not supported yet. REPORT BUGS
Please report bugs to fatsort(at)formenos.de. Thanks. FATSort 0.9.15 2011 fatsort(1)
All times are GMT -4. The time now is 03:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy