replacing characters in order to sort


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replacing characters in order to sort
# 1  
Old 08-01-2005
replacing characters in order to sort

hi,

i want to rename all the file names in order so that they can be sorted later.
For example, my filenames are like path\1, path\2...path\10, path\11.
But when I sort them, it sorts by the first number, so path\1 gets sorted with path\10.
I'm guessing the best way to do this is to rename the single digits to something like \01, \02...
So would this be the correct input?
sed 's///1///01/' file
sed 's///2///02/' file
sed 's///3///03/' file

and so on. A problem I might encounter with this is that if my filenames go into the
3-digits (ie. 330), then I'll have to rename them to 001, 002, and so on. any suggestions?

thanks,
Gammmaman
# 2  
Old 08-01-2005
If the files are all in one directory:
Code:
cd /path 
ls -1 | sort -n >> sorted.lis
cd -

# 3  
Old 08-01-2005
Quote:
Originally Posted by jim mcnamara
If the files are all in one directory:
Code:
cd /path 
ls -1 | sort -n >> sorted.lis
cd -

thanks for the reply. The sort I'm trying to do is done through a file that lists the filepaths. I don't have access to the actual path. Can this be done?

thanks,
gammaman
# 4  
Old 08-02-2005
Quote:
Originally Posted by gammaman
The sort I'm trying to do is done through a file that lists the filepaths. I don't have access to the actual path. Can this be done?

thanks,
gammaman
In that case, you just have to do this:
Code:
sort -n file_that_lists_the_filenames > sorted.lis

Note that I have said filenames, and not paths. You can use sed/awk to extract the filenames from the full paths.
# 5  
Old 08-02-2005
Quote:
Originally Posted by gammaman
hi,

i want to rename all the file names in order so that they can be sorted later.
For example, my filenames are like path\1, path\2...path\10, path\11.
But when I sort them, it sorts by the first number, so path\1 gets sorted with path\10.
I'm guessing the best way to do this is to rename the single digits to something like \01, \02...
So would this be the correct input?
sed 's///1///01/' file
sed 's///2///02/' file
sed 's///3///03/' file

and so on. A problem I might encounter with this is that if my filenames go into the
3-digits (ie. 330), then I'll have to rename them to 001, 002, and so on. any suggestions?

thanks,
Gammmaman

Using sed, you can't change the actual file names. Though the text file containing the files can be modified, later on, mapping between a name and its (supposedly) corresponding file will fail.

Say path/1 is the actual file name. In the text file, you change the name to path/01. Now you are talking about two different files viz. path/1 and path/01. So, I think it is better you pick up those 9 files and rename them. And then sort.



vino
# 6  
Old 08-02-2005
try a tag sort like this:
Code:
# create a tmpfile with two fields
#  short file name
#  full file name
> tmpfile
while read file
do
      echo "`basename $file`  $file" >> tmpfile
done < filelist
# sort the tmp file
sort -n -o tmpfile tmpfile
# print just the full file name in sorted order 
awk ' {print $2} ' tmpfile > newsortedfilelist

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort in chronological order

I am trying to sort a log file in chronological order to identify which ones did not process and still have an old (probably yesterday's) date. This is a sample of the file:flatf 010140 flatf Thu May 10 22:22:11 CST 2018 flats finished flatf 010142 flatf Thu May 10 22:31:25 CST 2018 flats... (4 Replies)
Discussion started by: wbport
4 Replies

2. Shell Programming and Scripting

Sort by specific order?

Hello all I was wondering if someone has an idea how to sort by a specific order, let's say by a specific alphabet containing only 4 letters like (d,s,a,p) instead of (a,b,c....z) ?? Cheers! (6 Replies)
Discussion started by: cabrao
6 Replies

3. Shell Programming and Scripting

Sort numeric order

Hi I am using this cat substitutionFeats.txt | gawk '{$0=gensub(/\t/,"blabla",1);print}' | gawk '{print length, $0}' | sort -n | sort -r and the "sort -n" command doesn't work as expected: it leads to a wrong ordering: 64 Adjustable cuffs 64 Abrasion- 64 Abrasion pas 647 Sanitized 647... (4 Replies)
Discussion started by: louisJ
4 Replies

4. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

5. Linux

SORT CARD for replacing characters.

Hi all, Am new to linux. Please advise me how to program a SORT CARD that relaces characters. For e.g., 201115456563040gh879245k 20112450911804Ads559672p 20112276533504{tg090669d 201123776591040ra008792y 20114452611704{wq47544q In the above data, I need to relace all { to 0 (am... (3 Replies)
Discussion started by: das.somik
3 Replies

6. UNIX for Dummies Questions & Answers

sort by column or specific order

Hi, I need to sort it by column or need it in a specific order... input is ===== uid=shashi country= india region =0 ph=0 uid= jon region= asia ph= 12345 country=0 uid = man country= india ph=2222 region=0 uid= neera region= asia ph= 125 country=0 output should be uid=shashi ... (1 Reply)
Discussion started by: hegdeshashi
1 Replies

7. 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

8. Shell Programming and Scripting

SORT order in Unix

I am converting mainframes JCL to be used in shell on a one to one basis... when i use the sort command unix does ascii sort as a result which numbers are first followed by charecters in the Ascending sort ... but themainframes uses the EBCDIC as result gives the charecters followed by numbers in... (5 Replies)
Discussion started by: bourne
5 Replies

9. UNIX for Dummies Questions & Answers

Sort - original order .... Help

Hi all, I want to sort a file based on the number in the 9th column I've tried both of the following commands sort -k 9,9n file_to_sort.dat sort +8 -n file_to_sort.dat both resulting in the same output which does sort col 9 nummerically but it doesn't output the lines in the original... (2 Replies)
Discussion started by: olga
2 Replies

10. UNIX for Dummies Questions & Answers

Sort / ascending order

What's the command to sort a file in ascending order and redirect the output to another file? Thanks!!!!!! (1 Reply)
Discussion started by: gyik
1 Replies
Login or Register to Ask a Question