Solved

Clarification in sort command in Unix

Posted on 2014-03-10
5
391 Views
Last Modified: 2014-05-06
Can anyone explain what are the actions performed by the below sort commands.

sort -t'|' tmp1 -o tmp2
sort -t'|' -k 4,4n -o current.data tmp2

Open in new window

0
Comment
Question by:gaugeta
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 39919718
First one does a alpha-numeric sort of the tmp1 file using the pipe (|) symbol as the field separator and outputs the results to the file tmp2.

The second one does a numeric sort of the 4th field (pipe delimited) of tmp2 and outputs the result to current.data
0
 

Author Comment

by:gaugeta
ID: 39919747
@Tintin : Thanks for the reply.

So does the first command does a generic sort of the entire file in ascending order.

I was wondering if this syntax would suffice : sort -t'|' -k 4n -o current.data tmp2 , in the above command why do we need an extra 4,4n
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 39919763
>So does the first command does a generic sort of the entire file in ascending order.

Correct.

-k 4n and -k4,4n are different.

-k 4n  means sort based on the fourth field to the end of the line.

-k 4,4n means sorted based on the fourth field only.
0
 

Author Comment

by:gaugeta
ID: 39919771
So if I have 10 fields separated by "|" does sort -t'|' -k 4n -o current.data tmp2  sort the file including colums from the 4th-10th.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39922201
Correct.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now