Problem with sorting in shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with sorting in shell script
# 1  
Old 11-27-2013
Problem with sorting in shell script

Hi,

I have the following list:

Code:
42A
42AA
42B
42BB
42AAA
42BBB
49A
49AA
49B
49AAA
49BB

I need it to be sorted as following:

Code:
42A
42B
42AA
42BB
42AAA
42BBB
49A
49B
49AA
49AAA
49BBB

Please help me with this.

Last edited by Don Cragun; 11-27-2013 at 04:57 AM.. Reason: Add CODE tags.
# 2  
Old 11-27-2013
The awk script pair:
Code:
awk '{printf("%02d%02d%s\n", substr($0,1,2), length($0)-2, substr($0,3))|"sort"}' list |
awk '{printf("%02d%s\n", substr($0,1,2), substr($0,5))}'

seems to do what you want, but produces:
Code:
42A
42B
42AA
42BB
42AAA
42BBB
49A
49B
49AA
49BB
49AAA

instead of
Code:
42A
42B
42AA
42BB
42AAA
42BBB
49A
49B
49AA
49AAA
49BBB

Note that 49BB appears in your input file, but 49BBB does not.

If you want to run this on a Solaris/SunOS system replace both calls to awk with calls to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-27-2013
Thanks for your reply. It works fine.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You will write a script that will read a tab-separated file that contains the names of all 50 states &... (7 Replies)
Discussion started by: tburns517
7 Replies

2. UNIX for Advanced & Expert Users

Sorting problem

I have data like this in file Now I have to sort using second column(usage) and i should get first column against it post sorting please provide input (2 Replies)
Discussion started by: mirwasim
2 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. UNIX for Dummies Questions & Answers

Sorting problem

1) I need to reverse sort the text from lit.csv (bellow) by the second column, then save the last five lines in a text file text.txt but I'm doing something wrong so any help would be much appreciated. I've been trying to use this: sort --field-separator=; -b -k2g,2 lit.csv -o text.txt 2)... (4 Replies)
Discussion started by: Kova
4 Replies

5. Shell Programming and Scripting

call shell script from perl cgi script problem

hi,, i have perl scipt with line : system('./try.sh $t $d $m'); in shell scipt try.sh i have the line: echo $1 its not printing value of $t that i hav passed..y is it so..i am running it from apache web server (2 Replies)
Discussion started by: raksha.s
2 Replies

6. Shell Programming and Scripting

shell script help: sorting, incrementing environment variables and stuff

First: me == noob. Whats a good resource for shell script info cause I'm having trouble finding good info. I'm writing a shell script to automate the setup of a flash 'page flip'. My current code is below. the page flip takes an xml file of format <content> <pages... (1 Reply)
Discussion started by: secoif
1 Replies

7. UNIX for Dummies Questions & Answers

Sorting problem

Hai , In unix we are going to sort it out all the directories and files based on size of the file. For that we have to use this command ls -al | sort +4nr. If we are giving this command means it will show all the records in a descending order....when I am checking one file bytes its... (4 Replies)
Discussion started by: cool4naren
4 Replies

8. HP-UX

Sorting problem....

hey mate. ive got a sorting problem that i would like to share... i made a script that sorts the bdf command and redirected the output to a file. the output of the file is: 691416 34% / 851552 7% /stand 7203048 31% /var 23637584 26% /var/adm/crash 2968864 ... (4 Replies)
Discussion started by: jarod004
4 Replies

9. Shell Programming and Scripting

sorting/arrangement problem

Hi, I have the following 'sorting' problem. Given the input file: a:b:c:12:x:k s:m:d:8:z:m a:b:c:1:x:k p:q:r:23:y:m a:b:c:3:x:k p:q:r:1:y:m the output I expect is: a:b:c:1:x:k p:q:r:1:y:m s:m:d:8:z:m What happened here is I grouped together lines having the same values for... (7 Replies)
Discussion started by: Abhishek Ghose
7 Replies

10. Shell Programming and Scripting

Problem in sorting the file

file format is word filename no.of occurances ------------------------------ wish f3.txt 2 wish f2.txt 1 cold f1.txt 5 cold f2.txt 3 cold f1.txt 3 cold e.txt 3 gold f1.txt 7 gold f3.txt 3 rush e.txt 2 itz abt building a search index for every word in the files given as input the... (1 Reply)
Discussion started by: vishnu_vaka
1 Replies
Login or Register to Ask a Question