LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-25-2014, 11:48 PM   #1
a_bahreini
LQ Newbie
 
Registered: Mar 2014
Posts: 7

Rep: Reputation: Disabled
sorting columns individually


Hi,
I have a file full of this fields and rows (this is showing two rows and two columns but my file has more rows and columns):
Code:
0.269330|0.035118|0.526763|0.792274         0.33555|19.471911|51.844968|1631  ...
3.981490|5.062725|17.190744|111     0.000000|0.030234|0.000000|1631  ...
...
I'd like to sort EACH column individually based on the value after the third "|". This has to be done separately for each column. Is there anybody to help? BTW, the file is tab delimited.
Thanks
 
Old 03-26-2014, 12:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You may need to explain a little further as it does not appear clear what you require (at least to me).

If you have 2 rows by 2 columns, what do the '...' represent?

If you are using a single field as the reference, you then say you want to sort each column based on this. If there is
only one reference point, won't all the columns end up in the same position after sorting?
 
Old 03-26-2014, 12:07 AM   #3
a_bahreini
LQ Newbie
 
Registered: Mar 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Sorry for the confusion. "..." means just that I have more of those fields and rows, please ignore those. My file has 41 fields and 60 rows. I have only inputed two rows and two columns in my last post. I don't want to sort based on a particular field but I'd like to sort each column by itself based on the number after third vertical bar on that column. I just wanted 41 sorted columns in my output. Hope this helped
 
Old 03-26-2014, 12:35 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
In the field after the 3rd '|' you have a field that has "number< some spaces >anothernumber"
That looks really odd....

How about posting some real data eg 5 cols, 5 rows and use CODE tags https://www.linuxquestions.org/quest...do=bbcode#code and possibly re-phrase the qn.
Thx.

Last edited by chrism01; 03-26-2014 at 12:37 AM.
 
Old 03-26-2014, 12:40 AM   #5
a_bahreini
LQ Newbie
 
Registered: Mar 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Chrism01,
That's not "some space" but a "tab". The file is tab delimited. Each field has a bunch of numbers separated by "|".

Last edited by a_bahreini; 03-26-2014 at 12:46 AM.
 
Old 03-26-2014, 01:52 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
ok ... using your current example (although I think chrism01 might be right that a couple more lines may help)
please show what the output would look like after the sort has been completed?
 
Old 03-26-2014, 02:12 AM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,818

Rep: Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408
for small things like this using a spread sheet is likely the best tool

excel or the Openoffice Equiv. " Calc" is very good at things like this
 
Old 03-26-2014, 04:41 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
This
[quote]
sort each column by itself
[/code]
makes sense.

This
Code:
based on the number after third vertical bar on that column
not so much ...
Do you mean 'on that row' ?
Also, which number, if each field has multiple numbers; 1st, 2nd, 3rd ...?


As per grail, we need example o/p after sorting.
 
Old 03-26-2014, 09:39 AM   #9
a_bahreini
LQ Newbie
 
Registered: Mar 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Ok, let's make this simple. Here is two columns of my file:
Code:
2|3|40|50	21|32|60|70
12|40|30|60	34|21|50|80
43|33|20|21	54|23|70|56
Here I sort each column based on the third number (bold after the second "|"):
Code:
43|33|20|21	34|21|50|80
12|40|30|60	21|32|60|70
2|3|40|50	54|23|70|56
I can't use excel since I have too many numbers in each field separated with "|" and I will end up with tons of columns if replace "|" with "\t".
Thanks in advance
 
Old 03-26-2014, 09:49 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
what have you tried and where are you stuck ?
i would cut each grouping into its own file
then sort by the 3rd key delimited by '|'
then paste the result back to 1 file separated by tab.
 
1 members found this post helpful.
Old 03-26-2014, 09:56 AM   #11
a_bahreini
LQ Newbie
 
Registered: Mar 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks schneidz for the solution but can I do this in a single command line? How can I "cut" all the columns into the individual fields?
 
Old 03-26-2014, 09:58 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
man cut
 
Old 03-26-2014, 10:33 AM   #13
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 505

Rep: Reputation: 137Reputation: 137
You can make use of multiple delimiters in awk. something like this...

Code:
$ cat file
4.269330|0.035118|0.526763|0.792274	0.33555|19.471911|51.844968|1631
3.981490|5.062725|17.190744|121		4.12300|0.030234|0.000000|1631
6.269330|0.035118|0.526763|0.392274	1.67525|19.471911|51.844968|1631
2.269330|0.035118|0.526763|0.992274	88.55|19.471911|51.844968|1631
8.981490|5.062725|17.190744|511		0.000000|0.030234|0.000000|1631
0.981490|5.062725|17.190744|007		3.1|0.030234|0.000000|1631

$ awk -F'[|\t]+' '{print $4 | "sort -g"}' file
0.392274
0.792274
0.992274
007
121
511
 
Old 03-26-2014, 10:43 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Based on post #3, I would use your favourite tool (awk, perl, ruby, ...) and create 41 arrays / hashes with 60 elements using the third pipe separated number as the index.
Then either sort or regurgitate the numbers in the order required.

This would require that none of the pipe separated third fields are repeated ... can you guarantee this?
If you cannot, how is the sort supposed to know where to look after this field?
 
Old 03-26-2014, 11:03 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,928

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by a_bahreini View Post
Thanks schneidz for the solution but can I do this in a single command line? How can I "cut" all the columns into the individual fields?
You can POST WHAT YOU HAVE DONE AND TRIED SO FAR, as you were asked to. So far, you've posted a question, but have shown no effort of your own..not even reading the man pages on the commands you were given, which would TELL YOU how to do this.
 
  


Reply


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 suggestions to convert horizontal columns to vertical columns Guyverix Programming 14 01-24-2013 12:03 PM
[SOLVED] sorting alphanumeric columns captainentropy Programming 5 09-14-2011 02:14 PM
sorting text file on different columns in different orders arshadul Linux - Newbie 1 10-19-2009 03:15 PM
Bash script sorting multple columns together KBriggs Linux - Newbie 2 08-13-2009 01:59 PM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 01:22 AM


All times are GMT -5. The time now is 10:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration