Solved

Clarification in sort command in Unix

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

717 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