Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-12-2009, 01:33 PM   #1
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 90

Rep: Reputation: 16
cut and sort incompatible

chkconfig --list | sort -k5,5r -k1,1 | cut -f1,5
works fine, but:
chkconfig --list | cut -f1,5 | sort -k2,2r -k1,1
does not sort by field 1.

Please pop up any idea.
Old 07-12-2009, 02:41 PM   #2
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97

Last edited by Disillusionist; 07-12-2009 at 02:48 PM.
Old 07-12-2009, 04:59 PM   #3
Registered: Feb 2006
Distribution: Debian Sid
Posts: 695

Rep: Reputation: 275Reputation: 275Reputation: 275
It's a problem with delimiters. "sort" uses space(or consecutive spaces) and "cut" uses tab. The chkconfig output has no tab between the program name and the first on/off field.

Try these two commands:
chkconfig --list | sort  -t $'\t' -k5,5r -k1,1 | cut -f1,5
chkconfig --list |cut -f1,5 | sort -t $'\t' -k2r -k1,1
They make "sort" behave like "cut". I don't know how to make cut use multiple spaces as a delimiter. Maybe someone else does.

You can use awk instead of cut:
chkconfig --list |  awk '{printf("%-15s\t%s\n",$1,$5)}' |sort -k2r -k1,1
chkconfig --list | sort  -k5,5r -k1,1 | awk '{printf("%-15s\t%s\n",$1,$5)}'
Old 07-13-2009, 05:26 AM   #4
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I don't know what the output of chkconfig might look like, so I can't test this.
Old 07-13-2009, 11:02 AM   #5
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 90

Original Poster
Rep: Reputation: 16
norobro: Thank you for your help. You are right, it is a matter of delimiters and your examples are great.

One can display hidden characters in a file with vi file and :set list (:set nolist). spaces are always displayed as spaces, tabs are displayed either as alignment (nolist), or as ^I (list).

chkconfig --list inserts both spaces and then tabs after names, either of them just fine for correct alignment.

sort splits fields between a non-blank character to the left and a blank character to the right. blanks are spaces and tabs. cut splits fields at tabs. What happens here is that all spaces and tabs after names in chkconfig --list are preserved by cut and assigned to the second field by sort. Therefore, when I sort -k2,2r, all "on" fields are not equal at all, they are sorted according to their leading spaces. The 'incompatibility' between cut and sort is that cut's delimiter is tab and therefore it assigns blanks to the left field, while sort's delimiter is space and it assigns them to the right one.

Last edited by doru; 07-25-2009 at 09:46 AM. Reason: correction


cut, 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
php sort help - sort numerical descending then by alphabetical? RavenLX Programming 3 03-11-2009 09:35 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 11:12 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 08:44 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 09:40 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:46 AM.

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