?
Solved

Clarification in sort command in Unix

Posted on 2014-03-10
5
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to moveā€¦
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

800 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