Sorting the first 6 letters and the rest will follow

Hi Guys,
is there any way to sort a files from excel in shell?

$cat file1

asa003 hekk
asa341 no more
asa012 try a d g
asa001 a
asa111 this is just a sample for long string

Desire output:

asa001 a
asa003 hekk
asa012 try a d g
asa111 this is just a sample for long string
asa341 no more

Hello kenshinhimura,

Could you please try following and let me know if this helps you.

 sort -k1.1 file1
 

Output will be as follows.

asa001 a
asa003 hekk
asa012 try a d g
asa111 this is just a sample for long string
asa341 no more
 

Thanks,
R. Singh

1 Like

perfect sir

The command:

sort -k1.1 file

sorts file with a sort key that starts with the 1st character on each line and continues to the end of the line. What was requested was:

sort -k1.1,1.6 file

which sorts with a key being the 1st six characters of the 1st field on each line.
But, since we aren't doing a numeric sort and we aren't performing a reverse order sort, both of the above, and the simpler:

sort file

will all do exactly the same thing.

1 Like

Hi Don,
thanks,, what will ahppen if it s numeric..i tried to search in ggoogle..couldnt find exact answer

001asa003 hekk
100hasa341 no more
100asa012 try a d g
002asa001 a
039asa111 this is just a sample for long string
101sdfc
099sdfs
999
1441
102
1000


the always output no matter what kind of sort is

001asa003 hekk
002asa001 a
039asa111 this is just a sample for long string
099sdfs
1000
100asa012 try a d g
100hasa341 no more
101sdfc
102
1441
999


never been a smaller to bigger

Instead of searching Google for answers about how a utility works, try reading the man page for that utility on your system. In this case try the command:

man sort

to see the man page for the sort utility on your system. The behavior of lots of utilities varies from system to system. What you find on Google MIGHT or MIGHT NOT explain how the utility you're searching for will work on your system; the man page on your system WILL explain how the utility will work on your system.

If the man page on your system doesn't answer your question, or you don't understand what some of the words on the man page on you system mean; create a sample data file and try sorting with sort file (which your man page will explain performs an alphanumeric sort) and sort -n file (which your man page will explain performs a numeric sort). For example, if file contains:

sa003 hekk
ASA341 no more
asa012 try a d g
ASA001 a
asa111 this is just a sample for long string
000aaa note that a numeric sort treats sort keys starting with text as zero.
000AAA NOTE THAT A NUMERIC SORT TREATS SORT KEYS STARTING WITH TEXT AS ZERO.
001asa003 hekk
100hasa341 no more
100asa012 try a d g
002asa001 a
039asa111 this is just a sample for long string
101sdfc
099sdfs
999
1441
102
1000

then an alphanumeric sort ( sort file ) produces:

000AAA NOTE THAT A NUMERIC SORT TREATS SORT KEYS STARTING WITH TEXT AS ZERO.
000aaa note that a numeric sort treats sort keys starting with text as zero.
001asa003 hekk
002asa001 a
039asa111 this is just a sample for long string
099sdfs
1000
100asa012 try a d g
100hasa341 no more
101sdfc
102
1441
999
ASA001 a
ASA341 no more
asa012 try a d g
asa111 this is just a sample for long string
sa003 hekk

while a numeric sort ( sort -n file ) produces:

000AAA NOTE THAT A NUMERIC SORT TREATS SORT KEYS STARTING WITH TEXT AS ZERO.
000aaa note that a numeric sort treats sort keys starting with text as zero.
ASA001 a
ASA341 no more
asa012 try a d g
asa111 this is just a sample for long string
sa003 hekk
001asa003 hekk
002asa001 a
039asa111 this is just a sample for long string
099sdfs
100asa012 try a d g
100hasa341 no more
101sdfc
102
999
1000
1441

If you can't figure out why your examples aren't working the way you think they should, show us the sample input you used, the output you got, the code you used to transform that sample input to the output you got, show us the output you wanted to get (or expected to get), and explain what you didn't understand on the man page or what you were trying to do that didn't work.

We will be happy to help you figure out how to use the tools available on your system; we will quickly get tired of being asked to do someone else's work for them if they don't seem to be interested in learning how to do it on their own.