The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2002
samudimu samudimu is offline
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.
  #2 (permalink)  
Old 05-23-2002
Kevin Pryke Kevin Pryke is offline
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
samudimu samudimu is offline
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
Kevin Pryke Kevin Pryke is offline
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
samudimu samudimu is offline
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0