[Solved] Sorting a column based on another column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Sorting a column based on another column
# 1  
Old 12-14-2012
[Solved] Sorting a column based on another column

hello,

I have a file as follows:
Code:
F0100010 A C     F0100040 A G    BTA-28763-no-rs     77.2692
F0100020 A G      F0100030 A T    BTA-29334-no-rs     11.4989
F0100030 A T      F0100020 A G    BTA-29515-no-rs     127.006
F0100040 A G      F0100010 A C    BTA-29644-no-rs     7.29827
F0100050 A T      F0100050 A T    BTA-32647-no-rs     70.9005

I want to sort the fourth column based on the first column to get the same order.

Thank you in advance for any help.
# 2  
Old 12-14-2012
Please show us you expected output, based on the sample above.
# 3  
Old 12-14-2012
What is the difference between sorting on the fourth column, and sorting 'based on' the fourth column? Do you want to sort on both columns, but group on the fourth?

In short -- what output would you expect for this input?
# 4  
Old 12-14-2012
output:
Code:
F0100010 A C      F0100010 A C    BTA-29644-no-rs     7.29827  
F0100020 A G      F0100020 A G    BTA-29515-no-rs     127.006
F0100030 A T      F0100030 A T    BTA-29334-no-rs     11.4989
F0100040 A G      F0100040 A G    BTA-28763-no-rs     77.2692

This is what I want to have.

The difference is that if I only sort the fourth column, it will sort based on the numbers but I want to keep the same order as it exists in the first column. I can not sort the first column too because it is crucial to keep the order.
# 5  
Old 12-14-2012
Oh, I see. You don't want it sorted. You want columns 4 through n of all rows moved such that column 1 lines up with column 4.

Working on it.

Last edited by Corona688; 12-14-2012 at 12:29 PM..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-14-2012
Code:
$ awk 'NR==FNR { ARR[$4]=$0 ; next }; $1 in ARR { A=$1 ; B=$2; C=$3; D=$4 ; $0=ARR[$1]; $1=A; $2=B; $3=C } 1' datafile datafile

F0100010 A C F0100010 A C BTA-29644-no-rs 7.29827
F0100020 A G F0100020 A G BTA-29515-no-rs 127.006
F0100030 A T F0100030 A T BTA-29334-no-rs 11.4989
F0100040 A G F0100040 A G BTA-28763-no-rs 77.2692
F0100050 A T F0100050 A T BTA-32647-no-rs 70.9005

$

Yes, I put the input file into it twice, not a typo. The first time reads all lines into memory and indexes on the fourth column. The second time, it prints out lines, recalling and recombining lines.

Last edited by Corona688; 12-14-2012 at 12:42 PM.. Reason: fix mistake
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 12-14-2012
Great, thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

2. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies

6. UNIX for Dummies Questions & Answers

Sorting a file based on the absolute value of a column

I would like to sort a tab delimited text file based on the absolute value of its second column. How do I go about doing that? Thanks! Example input: A -12 B 0 C -6 D 7 Output: A -12 D 7 C -6 B 0 (4 Replies)
Discussion started by: evelibertine
4 Replies

7. Shell Programming and Scripting

[SOLVED] Sorting into new files based on column

Hi, I have the following file, I need to sort it based on a column and write to different output files based on this column request_guid iso_country_cd address_data response_time 32895901-d17f-414c-ac93-3e7e0f5ec240 AND BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND;... (1 Reply)
Discussion started by: ramky79
1 Replies

8. Emergency UNIX and Linux Support

[Solved] Extract records based on a repeated column value

Hi guys, I need help in making a command to find some data. I have multiple files in which multiple records are present.. Each record is separated with a carriage return and in each record there are multiple fields with each field separated by "|" what i want is that I want to extract... (1 Reply)
Discussion started by: m_usmanayub
1 Replies

9. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

10. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question