![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |