LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2018, 03:10 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
sort 2 columns


Hi,

I have the following print I get from SGE
Code:
35057546   16.0   13.0    r       10/07   06:52   be_main.q   cooper444   1       proj-unit1-dc-gen_rep         unit1_rtl_28_05_18_FP1_pins_oreder_fix_w_sss
35062872   6.0    NA      hqw     10/07   10:14   NA          NA          8       proj-unit1-cf        		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062828   8.0    NA      hqw     10/07   10:12   NA          NA          8       proj-unit1-coc      		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062840   9.0    NA      hqw     10/07   10:12   NA          NA          8       proj-unit1-coo     		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35012037   14.0   8.3     r       09/07   15:40   be_main.q   cooper321   1       proj-unit1--INTERACTIVE       unit1_rtl_28_05_18_FP1_pins_oreder_fix_w_sss
35062810   6.0    12.5    r       10/07   10:34   be_main.q   cooper311   8       proj-unit1-po          	unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062853   5.0    NA      hqw     10/07   10:13   NA          NA          8       proj-unit1-ra         	unit1_rtl_28_05_18_FP1_pins_oreder_fix
34949335   4.0    44.7    r       09/07   20:34   be_main.q   cooper343   8       proj-unit3-ra        		unit3_rtl_06_06_18_FP2
34949379   4.0    41.8    r       10/07   00:41   be_main.q   cooper358   8       proj-unit3-ra        		unit3_rtl_06_06_18_FP1
34949920   4.0    46.2    r       09/07   17:38   be_main.q   cooper341   8       proj-unit3-ra         	unit3_rtl_06_06_18_FP3
34949378   4.0    NA      hqw     08/07   11:09   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP2
34949384   4.0    NA      hqw     08/07   11:10   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP1
34949933   4.0    NA      hqw     08/07   11:23   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP3
35009686   22.0   36.1    r       09/07   14:45   be_main.q   cooper440   8       proj-unit2-dc                 unit2_rtl_18_05_18r1_FP2_w_sss
35062135   35.0   16.4    r       10/07   09:39   be_main.q   cooper423   1       proj-unit2-dcrep              unit2_rtl_18_05_18r1_FP2
34951419   4.0    NA      hqw     08/07   12:01   NA          NA          8       proj-unit2-cf        		unit2_rtl_18_05_18r1_FP1_scripts_change
35063469   4.0    NA      hqw     10/07   10:26   NA          NA          8       proj-unit2-cf        		unit2_rtl_18_05_18r1_FP2
35063190   16.0   10.3    r       10/07   10:26   be_main.q   cooper307   8       proj-unit2-Id        		unit2_rtl_18_05_18r1_FP2
35003338   14.0   20.4    r       09/07   11:26   be_main.q   cooper348   1       proj-unit2--INTERACTIVE	unit2_rtl_18_05_18r1_FP1_scripts_change
34951371   4.0    60.9    r       09/07   18:57   be_main.q   cooper348   8       proj-unit2-ro          	unit2_rtl_18_05_18r1_FP1_scripts_change
35063418   4.0    NA      hqw     10/07   10:25   NA          NA          8       proj-unit2-ro          	unit2_rtl_18_05_18r1_FP2
I want to ask how can I sort twice in same command line in tcsh.
First I want to sort by name in column 10 and then by column 7 but keeping column 10 sorted.
I tried to use below command but without success
Code:
awk 'NR == 0 ; NR > 1 {print $0 | "sort -k11,11 -k7,7" }'
This should be look like below example:
Code:
35057546   16.0   13.0    r       10/07   06:52   be_main.q   cooper444   1       proj-unit1-dc-gen_rep         unit1_rtl_28_05_18_FP1_pins_oreder_fix_w_sss
35012037   14.0   8.3     r       09/07   15:40   be_main.q   cooper321   1       proj-unit1--INTERACTIVE       unit1_rtl_28_05_18_FP1_pins_oreder_fix_w_sss
35062810   6.0    12.5    r       10/07   10:34   be_main.q   cooper311   8       proj-unit1-po          	unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062872   6.0    NA      hqw     10/07   10:14   NA          NA          8       proj-unit1-cf        		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062828   8.0    NA      hqw     10/07   10:12   NA          NA          8       proj-unit1-coc      		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062840   9.0    NA      hqw     10/07   10:12   NA          NA          8       proj-unit1-coo     		unit1_rtl_28_05_18_FP1_pins_oreder_fix
35062853   5.0    NA      hqw     10/07   10:13   NA          NA          8       proj-unit1-ra         	unit1_rtl_28_05_18_FP1_pins_oreder_fix
35009686   22.0   36.1    r       09/07   14:45   be_main.q   cooper440   8       proj-unit2-dc                 unit2_rtl_18_05_18r1_FP2_w_sss
35062135   35.0   16.4    r       10/07   09:39   be_main.q   cooper423   1       proj-unit2-dcrep              unit2_rtl_18_05_18r1_FP2
35063190   16.0   10.3    r       10/07   10:26   be_main.q   cooper307   8       proj-unit2-Id        		unit2_rtl_18_05_18r1_FP2
35003338   14.0   20.4    r       09/07   11:26   be_main.q   cooper348   1       proj-unit2--INTERACTIVE	unit2_rtl_18_05_18r1_FP1_scripts_change
34951371   4.0    60.9    r       09/07   18:57   be_main.q   cooper348   8       proj-unit2-ro          	unit2_rtl_18_05_18r1_FP1_scripts_change
34951419   4.0    NA      hqw     08/07   12:01   NA          NA          8       proj-unit2-cf        		unit2_rtl_18_05_18r1_FP1_scripts_change
35063469   4.0    NA      hqw     10/07   10:26   NA          NA          8       proj-unit2-cf        		unit2_rtl_18_05_18r1_FP2
35063418   4.0    NA      hqw     10/07   10:25   NA          NA          8       proj-unit2-ro          	unit2_rtl_18_05_18r1_FP2
34949335   4.0    44.7    r       09/07   20:34   be_main.q   cooper343   8       proj-unit3-ra        		unit3_rtl_06_06_18_FP2
34949379   4.0    41.8    r       10/07   00:41   be_main.q   cooper358   8       proj-unit3-ra        		unit3_rtl_06_06_18_FP1
34949920   4.0    46.2    r       09/07   17:38   be_main.q   cooper341   8       proj-unit3-ra         	unit3_rtl_06_06_18_FP3
34949378   4.0    NA      hqw     08/07   11:09   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP2
34949384   4.0    NA      hqw     08/07   11:10   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP1
34949933   4.0    NA      hqw     08/07   11:23   NA          NA          8       proj-unit3-ro          	unit3_rtl_06_06_18_FP3

Last edited by DoME69; 07-10-2018 at 03:30 AM.
 
Old 07-10-2018, 03:24 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please use "code" tags, not "quote" tags. Also please reduce the sample-lists, we do understand the concept of sorting, even without a dozen of lines.
 
Old 07-10-2018, 03:30 AM   #3
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
Please use "code" tags, not "quote" tags. Also please reduce the sample-lists, we do understand the concept of sorting, even without a dozen of lines.
Sorry,

Done.
 
1 members found this post helpful.
Old 07-10-2018, 03:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Thank you. I think it would be simply:
Code:
LC_ALL=C sort -k10 -k7 inputfile >outputfile
 
Old 07-10-2018, 04:24 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sort defaults to end-of-record if no termination field is included, so the OP has the right idea, but why -k11 ?.
I know nothing of tcsh, but I also don't understand the need for awk.
 
Old 07-10-2018, 04:56 AM   #6
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by syg00 View Post
sort defaults to end-of-record if no termination field is included, so the OP has the right idea, but why -k11 ?.
I know nothing of tcsh, but I also don't understand the need for awk.
I had to remove 2 rows from top this is why I use awk and then sort.
 
Old 07-10-2018, 04:58 AM   #7
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
Thank you. I think it would be simply:
Code:
LC_ALL=C sort -k10 -k7 inputfile >outputfile
This is not work for me.
I use the command line to sort jobs from SGE, below is the full command.

Code:
set LC_ALL="C"
qstat -showdir| egrep -v "idle.q|xmsg[00000-99999]|\-\-\-\-\-\-\-\-\-\-" | sort -k10 -k7
 
Old 07-10-2018, 05:01 AM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by DoME69 View Post
I want to ask how can I sort twice in same command line in tcsh.
First I want to sort by name in column 10 and then by column 7 but keeping column 10 sorted.
That seems a contradictory specification...if you want both col 7 and col 10 sorted, but with col 10 as the high order, don't you need to sort by 7 first, then 10?

I'd have to play with it some, but it's past midnight. I'll check back in the morning.

I'd use sort, not awk.

I'm not sure which shell is being used is a factor, either.
 
Old 07-10-2018, 05:07 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> This is not work for me.

Please explain this in more details. You may even create examples, eg:
Code:
#!/bin/sh

export LC_ALL=C

sort -k10 -k7 <<EOF
C1 C2 C3 C4 C5 C6 71 C8 C9 100
B1 B2 B3 B4 B5 B6 72 B8 B9 101
A1 A2 A3 A4 A5 A6 73 A8 A9 100
EOF
the result:
Code:
C1 C2 C3 C4 C5 C6 71 C8 C9 100
A1 A2 A3 A4 A5 A6 73 A8 A9 100
B1 B2 B3 B4 B5 B6 72 B8 B9 101

Last edited by NevemTeve; 07-10-2018 at 05:10 AM.
 
Old 07-10-2018, 05:23 AM   #10
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
> This is not work for me.

Please explain this in more details. You may even create examples, eg:
Code:
#!/bin/sh

export LC_ALL=C

sort -k10 -k7 <<EOF
C1 C2 C3 C4 C5 C6 71 C8 C9 100
B1 B2 B3 B4 B5 B6 72 B8 B9 101
A1 A2 A3 A4 A5 A6 73 A8 A9 100
EOF
the result:
Code:
C1 C2 C3 C4 C5 C6 71 C8 C9 100
A1 A2 A3 A4 A5 A6 73 A8 A9 100
B1 B2 B3 B4 B5 B6 72 B8 B9 101

As you can see in my open thread,
All name with the word unit* (column 10) sorted without effect in column 7 (name: be_main.q)
both columns keep sorted.
 
Old 07-10-2018, 05:35 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> All name with the word unit* (column 10)
Isn't that column 11 ?
 
Old 07-10-2018, 05:50 AM   #12
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
> All name with the word unit* (column 10)
Isn't that column 11 ?
yes.
 
Old 07-10-2018, 06:08 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then try both 'sort -k11 -k7' and 'sort -k7 -k11', if neither helps then you should explain your problem more clearly.
 
1 members found this post helpful.
Old 07-12-2018, 10:15 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I think your example is wrong, or impossible.

Last edited by bigearsbilly; 07-12-2018 at 10:24 AM. Reason: wrong
 
  


Reply



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
Sort by the one of the columns at a time hanae Programming 11 05-18-2012 04:18 AM
text manipulation in bash: sort columns according to the first row lethalfang Linux - Newbie 5 06-20-2011 04:10 PM
sort multiple columns + replace another column cedance Linux - Newbie 1 03-29-2011 05:59 AM
[SOLVED] How to Sort Columns of Data (descending to ascending)? msbstar Linux - Newbie 5 03-21-2011 12:08 PM
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 AM.

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
Open Source Consulting | Domain Registration