sort a file in reverse order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort a file in reverse order
# 8  
Old 09-21-2005
Abey,Seems these options are not in confirmation to any standards Smilie as I told the "-r" option works with solaris 5.8, which version of unix uses "-n" option?

rishi
# 9  
Old 09-21-2005
mine is HP-Unix 11, tht tac didn't worked on mine too....

abey
# 10  
Old 09-21-2005
Here is a non-tac solution. Courtesy sed one-liners

Hope you have a version of sed which will make the following work.

Code:
sed '1!G;h;$!d' sample.file

and yet another sed solution

Code:
sed -n '1!G;h;$p'  sample.file

vino

Last edited by vino; 09-21-2005 at 08:04 AM..
# 11  
Old 09-21-2005
Quote:
Originally Posted by RishiPahuja
Seems these options are not in confirmation to any standards Smilie
rishi
Rishi, how very true! Smilie

"tac" is a non-standard utility but linux at least has it.
"tail -r" is not in Posix, but Solaris has it (even SunOS 5.5.1 has it... I don't have access to an earlier version.)
"sort -r" will sort based on the content of the lines
"tail -n" requires an integer argument and does not reverse the lines

Those sed scripts are standard but they will overflow the size of the hold space for large files. HP-UX documents this as 8192 characters. Both SunOS and HP-UX coredump on large files. The scripts are very clever though! Smilie

Finally, I'm glad to see that no one wasted any time trying our search function. Because this thread is the very first time that anyone has encountered the problem of reversing the lines in a file, there is no point in trying to find an earlier solution. NOT! reversing a list
# 12  
Old 09-21-2005
Quote:
Originally Posted by Perderabo
Those sed scripts are standard but they will overflow the size of the hold space for large files. HP-UX documents this as 8192 characters. Both SunOS and HP-UX coredump on large files. The scripts are very clever though! Smilie
Yes, when dealing with large files it usually comes down to perl, so that being said:

Code:
perl -e 'print reverse <>' < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. Shell Programming and Scripting

Sorting a html file with an external sort order

I am working on a web-concordance of Old Avestan and my concordance has produced a HTML file The sort deployed by the HTML file is not something which we normally use. I have tried my best to force a sort within the concordance itself, but the sort order does not work. I am giving below the sort... (6 Replies)
Discussion started by: gimley
6 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

4. Shell Programming and Scripting

Reverse sort on delimited chunks within a file

Hello, I have a large file in which data of names is sorted according to their homographs. The database has the following structure:Each set of homographs with their corresponding equivalents in Devanagari is separated out from the next set by a hard return. An example will make this... (12 Replies)
Discussion started by: gimley
12 Replies

5. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

6. Shell Programming and Scripting

reverse sort file

Hi all I am trying to numerically reverse sort a file but I seem to be having trouble. Example of file contents: text1,1 text2,-1 text3,0 I can sort using sort -k 2n -t, filename without any problems. However I want my results in descending order but using -r in my command... (2 Replies)
Discussion started by: pxy2d1
2 Replies

7. Shell Programming and Scripting

Conditional reverse of certain file line order

Hi I am brand new to programming, I dont know how to go about this task, or what language is best for this...If there is an easy solution in different languages, I would love to see them. I want to learn about the steps to take on this, so Please put in comments where code is used. I believe in... (9 Replies)
Discussion started by: perlrookie
9 Replies

8. Shell Programming and Scripting

creating a file in reverse order of another file

Hi, I have a requirement where i need to write a script to create the new file from the given input file with the data in reverse order (bottom to top) Sample data: Input File-------------- Java VB Oracle Teradata Informatica Output file:----------------- Informatica Teradata Oracle... (3 Replies)
Discussion started by: srilaxmi
3 Replies

9. UNIX for Dummies Questions & Answers

sort -reverse order

I need to sort the particular column only in reverse order how i can give it.. if i give the -r option the whole file is getting sorted in reverse order. 1st 2nd col 3rd C col 4th col 5th col ------------------------------------------- C... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

10. Shell Programming and Scripting

read line in reverse order from file

dear all i want to read 5th no of line from last line of file. kindly suggest me possible ways. rgds jaydeep (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies
Login or Register to Ask a Question