LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-02-2017, 04:01 AM   #1
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 478

Rep: Reputation: 91
strange behaviour of the sort command with key


Hi,

I have a strange behaviour with the sort command.
I do have a file with package names and corresponding rpm files separated by semi-colon.
Code:
$ cat packages.txt
httpd-tools;httpd-tools-2.4.6-45.el7.centos.x86_64.rpm
httpd;httpd-2.4.6-45.el7.centos.x86_64.rpm
httpd-manual;httpd-manual-2.4.6-45.el7.centos.noarch.rpm
httpd-devel;httpd-devel-2.4.6-45.el7.centos.x86_64.rpm
I want to sort it by package names, and have an output such as below.
Code:
httpd httpd-2.4.6-45.el7.centos.x86_64.rpm
httpd-devel httpd-devel-2.4.6-45.el7.centos.x86_64.rpm
httpd-manual httpd-manual-2.4.6-45.el7.centos.noarch.rpm
httpd-tools httpd-tools-2.4.6-45.el7.centos.x86_64.rpm
But the command below does not give me the expected output.
Code:
$ sort -k 1 -t ';' packages.txt 
httpd-devel;httpd-devel-2.4.6-45.el7.centos.x86_64.rpm
httpd;httpd-2.4.6-45.el7.centos.x86_64.rpm
httpd-manual;httpd-manual-2.4.6-45.el7.centos.noarch.rpm
httpd-tools;httpd-tools-2.4.6-45.el7.centos.x86_64.rpm
I do not understand why httpd-devel is sorted before httpd.

If I isolate the package names then the sort command works as expected.
Code:
$ cut -f 1 -d ';' packages.txt | sort
httpd
httpd-devel
httpd-manual
httpd-tools
Any one with an explanation or a solution to my sorting need?
 
Old 03-02-2017, 04:10 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,827
Blog Entries: 3

Rep: Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398
If you have just -k 1 then it will start sorting at the first field and continue through the end of the line. It's rather buried in the manual, see KEYDEF, but if you tell it where to stop with -k 1,1 then it will sort as you expect.

Code:
$ sort -k 1,1 -t ';' packages.txt
You are thus telling it to sort only on the first field. However, that may not be what you want if you have more data to sort.

Code:
$ sort -k 1,1 -k 2 -t ';' packages.txt
That will tell it to first sort on the first field, but in cases of a tie sort on the rest of the line as well.
 
1 members found this post helpful.
Old 03-02-2017, 04:52 AM   #3
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 478

Original Poster
Rep: Reputation: 91
Hi Turbocapitalist,

You are my saviour.
Yes, I read the man page top to bottom several times, without picking the real meaning and purpose on KEYDEF.

So definitively, sort -k 1,1 -t ';' packages.txt does the job for me.
I do not expect two different packages to have identical names.

I closed the thread.
 
  


Reply

Tags
key, sort


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash sort strange behaviour... masavini Programming 5 06-23-2012 03:40 PM
Strange TC behaviour hesisaboury Linux - Networking 2 06-17-2012 02:20 AM
more command strange behaviour sancho1980 Linux - Newbie 2 03-12-2008 07:05 AM
ps -ef strange behaviour ?? lionking_x Linux - Newbie 1 11-28-2007 05:22 PM
Strange Behaviour! joshuarowley LQ Suggestions & Feedback 1 12-08-2005 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration