Sort - Help Needed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort - Help Needed
# 1  
Old 01-20-2006
Sort - Help Needed

Hi,

I am facing a small problem while I am doing the sort.

Problem Definition:

I have a file which has the following data.
>cat test.dat
zxzxc
asdasd
qwaxcZa
qwaxcA


When I do a sort on this file.

the result set is

asdasd
qwaxcA
qwaxcZa

zxzxc

But, we need to sort by using the first 5 charaters only.

The desired output should be

asdasd
qwaxcZa
qwaxcA

zxzxc

Can we do the sort by using only the first five characters of the field.

Any help is highly appriciated.

thanks
Anand
# 2  
Old 01-20-2006
Hi Anand,

Try this command
sort -k1.1,1.5 yourfilename

It means sort based on field 1 starting at first character to field 1 5th character.


Note: not tested
# 3  
Old 01-20-2006
Thanks for the reply, but its not working.

Still it gives the following output only.

> sort -k1.1,1.5 test.dat
asdasd
qwaxcA
qwaxcZa
zxzxc
# 4  
Old 01-20-2006
Try it with numeric option - possibly -g or -n (yea, I know it doesn't make sense)

Linux: sort -g -k1.1,1.5 yourfile
Solaris: sort -n -k1.1,1.5 yourfile
# 5  
Old 01-23-2006
Try this :

cat -n test.dat | sort -k2,2.5 -k1,1n | cut -f2-
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

2. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

3. Shell Programming and Scripting

sort command help needed

I have a file with three fields of data for example say we have 1420 $12.56 45 3456 $15.89 14 2341 $25.87 32 I need to use a command to show who is getting paid the highest amount to standard output. I am not sure if I should use a sort command or a copy and paste... (1 Reply)
Discussion started by: drew211
1 Replies

4. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

5. Shell Programming and Scripting

Help needed to sort data

Hello All, Today i have been asking lots of question, hope to become good in scripting soon with all the wonderful advices i get. The question is i want to sort data a get uniq string from it. The code i am using to generate the output is:- check_sun() { for i in $SUN_PLATFORM do $ECHO... (0 Replies)
Discussion started by: asirohi
0 Replies

6. Shell Programming and Scripting

Help needed with Sort and uniq data

Hi All, After Sorting directories and files i have got following output as below, now i only want the strings common in them, so the actual output should be as below in the bottom. How do i do that? Thanks -adsi File to be modified:- Common Components for ----> AA... (4 Replies)
Discussion started by: asirohi
4 Replies

7. Shell Programming and Scripting

sort command help needed.

Hello, suppose i have a student.dat file whose format is studentno studentname semester marks1 marks2 marks3 total then how can i sort first in semester wise and then total wise? I mean semester wise ranking wise record should be displayed. Semester should be displayed in ascending order... (6 Replies)
Discussion started by: salman4u
6 Replies

8. UNIX for Dummies Questions & Answers

Help needed to sort multiple columns in one file

Hi, I would like to know given that I have 3 columns. Let say I have first 3 columns to do operation and these operation output is printed out each line by line using AWK and associative array.Currently in the output file, I do a sort by -r for the operation output. The problem comes to... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

9. Shell Programming and Scripting

Help needed to sort file

I have a text file comprising of 3 columns 1. First column - numeric count/frequency of error codes 2. Second column - alphanumeric hyphenated error code 3. Third column - Error code description (alphanumeric) 184 ABCD-954 Errorcodedescription1 35645 DFMS-323 Errorcodedescription2 43534 ... (2 Replies)
Discussion started by: inditopgun
2 Replies

10. UNIX for Dummies Questions & Answers

help needed with sort. pipe and popen()

Suppose I want to find the number of uniq lines in a file. I use the following command: sort file1 | uniq -c | wc -l But if for some reason sort fails, the above command returns 0 as the answer. Why would sort fail ? sort makes use of directories /tmp or /var/tmp to store temporary files.... (7 Replies)
Discussion started by: the_learner
7 Replies
Login or Register to Ask a Question