Solved

Clarification in sort command in Unix

Posted on 2014-03-10
5
392 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

Suggested Solutions

Title # Comments Views Activity
installing LSI MegaRAID Storage Manager on CentOS 7 22 308
curl SSL certificate unable to get local issuer certificate 15 64
LINUX backups with VEEAM 8 79
awk variable in printf 1 24
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

776 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