Perl Sort on Text File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Sort on Text File
# 1  
Old 08-06-2007
Perl Sort on Text File

Hi,

I have a file of names and I want perl to do a sort on this file. How can I sort this list of names using perl? I'm thinking of a command like:

Code:
@sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort

The only thing I'm sort of unsure of is, how would I get the name in my text file into @sorted?

Also, would it be possible to use this perl code in the middle of a shell script? I think I remember someone telling me I could do something like this but I'm not sure of the tags I need to specify for this to work. Thanks!

Elt
# 2  
Old 08-07-2007
in order to work your command, you have to get the contents of file in the array @not_sorted. So read the file as follows:
Code:
open (NAMES_FILE, "< /path/to/file")  or  die "Failed to read file : $! ";
my @not_sorted = <NAMES_FILE>;  # read entire file in the array
close (NAMES_FILE);

now using your statement you would get the sorted names in the @sorted array.
This would be a Perl script, so you will have to invoke it from your shell script. But if you want to access the sorted list in your shell script, then you will have to store it somewhere (in a file).
Alternatively, think of doing the sorting using shell commands and tools. This way, you need not invokde a diffeerent Perl script.
# 3  
Old 08-07-2007
if it's in a shell script why not just use sort?
# 4  
Old 08-07-2007
Quote:
Originally Posted by Yogesh Sawant
in order to work your command, you have to get the contents of file in the array @not_sorted. So read the file as follows:
Code:
open (NAMES_FILE, "< /path/to/file")  or  die "Failed to read file : $! ";
my @not_sorted = <NAMES_FILE>;  # read entire file in the array
close (NAMES_FILE);

now using your statement you would get the sorted names in the @sorted array.
This would be a Perl script, so you will have to invoke it from your shell script. But if you want to access the sorted list in your shell script, then you will have to store it somewhere (in a file).
Alternatively, think of doing the sorting using shell commands and tools. This way, you need not invokde a diffeerent Perl script.
Cool thanks!

@bigearsbilly: I thought about that but I was told that the sort in shell does funny things and that perl would be more reliable. Maybe I'll try that and check out the results
# 5  
Old 08-07-2007
Sorry one more question. I got the file sorted and did a

print @sorted;

to verify it. How do I save the array @sorted into a file? Not sure of the operator or if there even is an operator for that in PERL. Thanks!
# 6  
Old 08-07-2007
Something like this,

each of the array contents should be flushed to the file

Code:
open(FILE, ">", "write" ) || die "unable to open file write <$!>";
  
  foreach(@sorted) {
    print FILE "$_";
  }
  
  close(FILE);

# 7  
Old 08-07-2007
Cool, Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort a text file if certain columns are blank?

Dear all, I am trying to sort a text file based on column 3, 10, 11 and 12. But certain column are blank for some lines. Column 3 has to be in ascending order after sorting. Part of my input file is as follows: CN727990 1 A01 4703 5083 73.28 - A_scaffold000011 4365605 4365985 73.28 +... (10 Replies)
Discussion started by: huiyee1
10 Replies

2. UNIX for Dummies Questions & Answers

Sort text file starting at column X

Hello everyone! As the heading reads, I would like to sort the lines of a text file, starting at a specific column (i.e. skip the first X characters of each line). What I’m actually trying to sort is the md5 sums file of a directory. Every time I copy a new file to that directory, I perform... (3 Replies)
Discussion started by: iznogoud
3 Replies

3. Shell Programming and Scripting

Perl SFTP, to get, sort and process every file.

Hi All, I'm niks, and i'm a newbie here and newbie also in perl sorry, i'm just wondering how can i get the file from the other hostname using sftp? then after i get it i'm going to sort the file and process it one by one. sorry because i'm a newbie. Thanks, -niks (4 Replies)
Discussion started by: nikki1200
4 Replies

4. Shell Programming and Scripting

sort each column of text file alone

Hello , i have a text file like this 1 a1 ,AB ,AC ;AD ,EE 2 a2 ,WE ;TR ,YT ,WW 3 a3 ;AS ,UY ;RF ,YT i want to sort this text file based on each row , and excluding 2nd column from the sorting and not taking the comma or ; into consideration in the sorting, so it will become like this... (12 Replies)
Discussion started by: shelladdict
12 Replies

5. Shell Programming and Scripting

Perl script to sort an Excel file

Hello! I need to sort a file that is partly in English partly in Bulgarian. The original file is an Excel file but I converted it to a tab-delimited text file. The encoding of the tab delimited file is UTF-8. To sort the text, the script should test every line of the text file to see if... (9 Replies)
Discussion started by: degoor
9 Replies

6. Shell Programming and Scripting

sort text file

HI all i have a text file file1 like this 004002004545454000001 041002004545222000002 006003008751525000003 007003008751352000004 006003008751142000005 004001005745745000006 i want to sort the file according to position 1-5 and secondary sort by the last position of file 16-21... (4 Replies)
Discussion started by: naamas03
4 Replies

7. Shell Programming and Scripting

Need a Help with sort a text file with some fields

Ive got a file called listacdrs with this structure: 01/09/2006 12:13 p.m. 1.046.528 CF0155.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0156.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0157.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0158.DAT 01/09/2006 12:14 p.m. ... (3 Replies)
Discussion started by: alexcol
3 Replies

8. Shell Programming and Scripting

Perl find::file can I sort the out put

Perl file::find can I sort the out put I am using file::find in my script but how I wish to process each file found in date order. Can I sort this module? eg part of current script is.... use File::Find; # Recursively find all files and directories in $mqueue_directory find(\&wanted,... (2 Replies)
Discussion started by: Andrek
2 Replies

9. Shell Programming and Scripting

sort a file by date using perl

Hello, do any body help me to sort a file by date using perl? thanks in advance Esham (4 Replies)
Discussion started by: esham
4 Replies

10. Shell Programming and Scripting

Sort file in perl

Hi, I have an entry file for a perl script from which I need to remove duplicate entry. For example: one:two:three one:four:five two:one:three must become : one:two:three two:one:three The duplicate entry is only the first field. I try many options of sort system command but don't... (4 Replies)
Discussion started by: annececile
4 Replies
Login or Register to Ask a Question