Sorting the first 6 letters and the rest will follow


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting the first 6 letters and the rest will follow
# 1  
Old 06-11-2015
Sorting the first 6 letters and the rest will follow

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

Code:
$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:
Code:
asa001 a
asa003 hekk
asa012 try a d g
asa111 this is just a sample for long string
asa341 no more


Last edited by Scrutinizer; 06-11-2015 at 05:30 PM.. Reason: Additional CODE tags
# 2  
Old 06-11-2015
Hello kenshinhimura,

Could you please try following and let me know if this helps you.
Code:
 sort -k1.1 file1

Output will be as follows.
Code:
asa001 a
asa003 hekk
asa012 try a d g
asa111 this is just a sample for long string
asa341 no more

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-11-2015
perfect sir
# 4  
Old 06-11-2015
The command:
Code:
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:
Code:
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:
Code:
sort file

will all do exactly the same thing.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-12-2015
Hi Don,
thanks,, what will ahppen if it s numeric..i tried to search in ggoogle..couldnt find exact answer

Code:
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
# 6  
Old 06-12-2015
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:
Code:
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:
Code:
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:
Code:
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting mixed numbers and letters

Hello, I have a file such as this: chr1 chr2 chr1 chr2 chr3 chr10 chr4 chr5 chrz chr1AI want to sort it, I use this command: sort -k1 -th -n testfilebut I get this output, how can I fix this? chr1 chr1 chr10 chr1A chr2 chr2 (3 Replies)
Discussion started by: Homa
3 Replies

2. Shell Programming and Scripting

Set Outlook property to Follow-up

Hi Unix Experts, I am trying to send mail from Unix to my email id configured on outlook. I need to make sure it works as a follow-up. I am using the below code my AIX unix box. /usr/sbin/sendmail -t << EOM X-Priority: 1 X-Message-Flag: Follow up Reply-By: 15/06/2011 10:00... (1 Reply)
Discussion started by: bankimmehta
1 Replies

3. OS X (Apple)

Does MAC OSX follow POSIX?

i mean if i coded an application for Linux using System Calls and no libs, and compiled it on MAC, will it work? thanks (6 Replies)
Discussion started by: JonhyM
6 Replies

4. Linux

How to follow up disk mirroring process?

Hi, The OS is SuSE Enterprise 11 and the system is HP WS460c G6 Blade with hardware disk array RAID 1 mirror. One disk was just replaced and the disk mirroring process is on its way. My question is how to follow up / monitor the disk mirroring process? I know hpacucli can do the job, but there... (0 Replies)
Discussion started by: aixlover
0 Replies

5. Shell Programming and Scripting

Yes/No Then Follow Command

Hello Guys, I'm currently creating a script for a couple of things and I'm semi stuck on where to go next. This is the script i've written but its not working in the exact way i want it to work. #!/bin/bash while : do clear echo " M A I N - M E N U" echo "1. Make a New... (1 Reply)
Discussion started by: matt02
1 Replies

6. UNIX for Advanced & Expert Users

How to follow processes and their children with ps

Hi, I often need to find the child processes of a parent process. There may be a string of 4-5. That is, PPID 884 spawns 890, which spawns 894, which spawns 1017. I'd like to be able to see all of them without having to type in a number of ps -ef commands. Process groups and session ID's are... (2 Replies)
Discussion started by: mschwage
2 Replies

7. Shell Programming and Scripting

Follow-up w/ Perderabo re: mimetool

This is a follow-up re: this thread As I mentioned, this script works very nicely, thanks again. However, the ASCII data of the 'attached file' also shows below the body of the email message. Do you know of a way to 'disable' the attached text from showing? Although the file is attached,... (3 Replies)
Discussion started by: jwperry
3 Replies
Login or Register to Ask a Question