Sorting filenames by order in another file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sorting filenames by order in another file
# 1  
Old 05-23-2002
Question Sorting filenames by order in another file

I would like to arrange /sort filenames ending with suffix like ".00XXXX". where X is a digit. However the order of arrangement is in a text file and is 'harpharzard'. e.g the text file may be like
002345
009807
001145
I wanted to avoid doing this using sql and exporting the text file back to unix.
This User Gave Thanks to samudimu For This Post:
# 2  
Old 05-23-2002
to sort a file (file.txt) containing
aaa.002345
bbb.009807
ccc.001145

sort -t "." +1 file.txt > file2.txt

would produce a file (file2.txt) containing
ccc.001145
aaa.002345
bbb.009807

the -t "." specifies that the field delimiter is ".", the +1 tells sort to use the 2nd field. > redirects the output to file2.txt
# 3  
Old 05-24-2002
You Missed it Kevin!

I do not simply want to sort. I would like to arrange them in an order given in a particular file. Which is NOT necessarily like 123... or abcd...
e.g.
Filenames are a.0011234
d.0023456
h.0032456
b.0025789

and the required order in the (sorting/arranging) file is
0023456
0032456
0011234
0025789

The output file should then be
d.0023456
h.0032456
a.0011234
b.0025789

I know chances are you will need two nested loops.
# 4  
Old 05-24-2002
This works, there may be some better awk/sed ways of doing it, I rarely use them so I'm not sure.

#!/bin/sh
>grepn.out
>linenumbers

cut -c3- file.in > cutfile.in

for record in `cat cutfile.in`
{
grep -n $record file.order >> grepn.out
}
cut -c1 grepn.out >> linenumbers

paste file.in linenumbers > file.tmp

sort +1 file.tmp > file.tmp2

awk '{print $1}' < file.tmp2 > file.out
# 5  
Old 05-24-2002
MySQL You Got it!

Thanx Kevin,
Howver even only part of your soln still works well!

grep -n $record file.order >> grepn.out

just replace "-n $record file.order " with "-f file.order"
NOTE grep with the -f option makes a wonderfull loop through a file.

MORE FIRE!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issues with sorting in reverse order

I have a unix script that outputs a summary file to the mac desktop. The file is called summary.txt I am trying to configure such so that the summary.txt file lists the content contained within such in reverse sort order. I have used sort -r but it does not seem to work. I would be... (8 Replies)
Discussion started by: Braveheart
8 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

Sorting a file in descending order when you have 10e- values

Hi, I am trying to sort the following file in descending order of its fourth column. 2 1 363828 -2.423225e-03 3 1 363828 4.132763e-03 3 2 363828 8.150133e-03 4 1 363828 4.126890e-03 I use sort -k4,4g -r input.txt > output.txt ... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX for Dummies Questions & Answers

sorting sequences in ascending order

Hi, I have this single file with a number of sequence inside it of format >string1 data >string100 data >string10 ..... >string5 ... >string67 ...... the dots represent data. I wanted to get the sequences arranged in ascending order like >string1 data >string5 (5 Replies)
Discussion started by: sonia102
5 Replies

5. UNIX for Dummies Questions & Answers

sorting PID in decreasing order using ps?

I am trying to sort the output in decreasing order of the process ID while using the ps command. I am having trouble doing this using the --sort part of ps. Also I was wondering if anyone knows what the "S" stands for under the process's status code? (3 Replies)
Discussion started by: crimputt
3 Replies

6. UNIX for Dummies Questions & Answers

Sorting of alphanumeric filenames..

Hi All, I want sort following files in ascending order. exp_installer_CC4160-file10.dmp exp_installer_CC4160-file11.dmp exp_installer_CC4160-file12.dmp exp_installer_CC4160-file13.dmp... (3 Replies)
Discussion started by: gramakrishna
3 Replies

7. Shell Programming and Scripting

Read filenames in sorted order

Hi , My requirement is to scan a directory for file names with LTR.PDF* and send those files via ftp to another server one by one. Now the the problem is file names are like LTR.PDF ,LTR.PDF1 ,LTR.PDF2.....LTR.PDF10..upto 99 and these needs to be sent in sorted order. is there a way to get... (10 Replies)
Discussion started by: nishantrk
10 Replies

8. Shell Programming and Scripting

multiple sorting with different order

Hi Guys, I have data like this HOS05 23/12/2008 10AM HOS06 15/12/2008 2PM HOS62 29/12/2008 10AM HOS64 23/12/2008 2PM HOS70 26/12/2008 10AM ZFT01 06/12/2008 10AM HOS73 11/12/2008 2PM MHOS0 05/12/2008 10AM MHOS0 20/12/2008 2PM MHOS0 27/12/2010 2PM MHOS0 11/12/2008 10AM MHOS0 30/12/2009... (1 Reply)
Discussion started by: ckarunprakash
1 Replies

9. Shell Programming and Scripting

Sorting a list of filenames but keeping the path information.

Hi All I've googled around for this and can't see a way of doing it. I have a file that contains a number of records that are layed out something like the following. /path/to/directory/that/contains/a/file/I/need/filename.pdf The path itself can vary both in terms of the names and the... (7 Replies)
Discussion started by: Bashingaway
7 Replies

10. UNIX for Dummies Questions & Answers

Order sorting

How do you sort text in order using sed? :confused: For example 01 B D A C to 01 ABCD (3 Replies)
Discussion started by: evoGage
3 Replies
Login or Register to Ask a Question