The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting Files by date and moving files in date order rebel64 Shell Programming and Scripting 2 03-11-2008 08:45 AM
bash: reading filenames from file warp17 Shell Programming and Scripting 0 03-04-2008 02:28 AM
shell program for sorting strings in an alphabetical order bp_vanarse Shell Programming and Scripting 1 10-25-2006 07:41 AM
Order sorting evoGage UNIX for Dummies Questions & Answers 3 12-04-2005 09:45 AM
Reversing file order using SED MBGPS Shell Programming and Scripting 2 09-30-2002 06:43 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-23-2002
Registered User
 

Join Date: Feb 2002
Location: Harare, Zimbabwe
Posts: 7
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.
Forum Sponsor
  #2 (permalink)  
Old 05-23-2002
Registered User
 

Join Date: Feb 2002
Location: Bradford, UK
Posts: 70
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 (permalink)  
Old 05-24-2002
Registered User
 

Join Date: Feb 2002
Location: Harare, Zimbabwe
Posts: 7
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 (permalink)  
Old 05-24-2002
Registered User
 

Join Date: Feb 2002
Location: Bradford, UK
Posts: 70
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 (permalink)  
Old 05-24-2002
Registered User
 

Join Date: Feb 2002
Location: Harare, Zimbabwe
Posts: 7
Thumbs up 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!
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0