Sponsored Content
Top Forums Shell Programming and Scripting Sort and join multiple columns using awk Post 302763915 by neutronscott on Wednesday 30th of January 2013 04:20:54 PM
Old 01-30-2013
Code:
mute@clt:~/temp/quincyjones$ ./script
a1      0       1       1       0       0
a2      1       0       1       0       0
a3      1       0       1       1       0
a4      1       1       0       1       0
b1      1       0       0       1       0
b2      1       0       0       1       1
c4      1       0       1       0       1
d1      1       1       0       0       1

Code:
#!/bin/sh
awk 'BEGIN { FS=OFS="\t"; }
{
        if (NF > maxNF) maxNF=NF
        for (i=1;i<=NF;i+=2) {
                if (length($i) < 1) continue #skip blanks
                #new, map it
                if (!name_to_idx[$i]) {
                        idx_to_name[++idx]=$i
                        name_to_idx[$i]=idx
                }
                arr[$i,i]=$(i+1) #make matrix
        }
}
END {
        for (i=1;i<=idx;i++) {
                printf("%s",idx_to_name[i])
                for (j=1;j<maxNF;j+=2)
                        printf("%s%d", OFS, arr[idx_to_name[i],j])
                print "" #newline
        }
}' file | sort -k1,1


Last edited by neutronscott; 01-30-2013 at 05:28 PM.. Reason: it should work now?
This User Gave Thanks to neutronscott For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or python to join alternating columns

Hi, here is my input TEST-SAM-A6ZZ SM-S6KV 0 0 2 1 2 3 3 3 2 2 2 2 1 1 2 2 1 3 1 3 1361 SM-HA4Q 0 0 2 1 3 3 3 3 2 2 2 2 1 1 2 2 1 3 3 3 4 2 4 2 2 276217 SM-H9ZG 0 0 2 2 3 3 3 3 2 2 2 2 1 1 2 2 3 3 1 3 2 2 2 2 GC15458 SM-HAQX 0 0 2 1 2 3 1 3 2 2 2 2 1 1 2 2 1 3 1 3 2 2 2 18331 SM-HA5E 0 0 2 2... (2 Replies)
Discussion started by: genehunter
2 Replies

2. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

3. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies

4. Shell Programming and Scripting

sort on multiple columns

Howdy! Need to sort a large .txt file containing the following, using sort. First based on the 1st column, and then on the 2nd column: Group01.01 1000500 31 0.913 -1.522974494 Group01.01 1001500 16 0.684 -0.967496041 Group01.01 36500 19 0.476 na Group01.02 365500 15 0.400 na... (1 Reply)
Discussion started by: sramirez
1 Replies

5. Shell Programming and Scripting

sort on multiple columns

hi all, i have a file , having few columns. i wanted to sort it based on 2nd column and then based on 1st column. But i have some problem in first column. first column have characters and numbers, but problem is number of characters are not same in all rows. Few rows have 13 characters and then... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

6. Programming

Ls -ltr Sort multiple columns

Hi All, I have one requirement, where I need to have output of ls -l command sorted on 1) first on filename 2) last modified time ( descending ) - latest change first. I am not able to figure out how to do it.. Also I dont have a way to change Date display for ls -ltr command.. I am... (1 Reply)
Discussion started by: freakabhi
1 Replies

7. Shell Programming and Scripting

Join two files combining multiple columns and produce mix and match output

I would like to join two files when two columns in each file matches with each other and then produce an output when taking multiple columns. Like I have file A 1234,ABCD,23,JOHN,NJ,USA 2345,ABCD,24,SAM,NY,USA 5678,GHIJ,24,TOM,NY,USA 5678,WXYZ,27,MAT,NJ,USA and file B ... (2 Replies)
Discussion started by: mady135
2 Replies

8. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

9. Shell Programming and Scripting

Join and merge multiple files with duplicate key and fill void columns

Join and merge multiple files with duplicate key and fill void columns Hi guys, I have many files that I want to merge: file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: (5 Replies)
Discussion started by: yjacknewton
5 Replies

10. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies
Perl::Critic::Policy::ControlStructures::ProhibitLabelsWUserpContriPerl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames(3)

NAME
Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames - Don't use labels that are the same as the special block names. AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
When using one of the special Perl blocks "BEGIN", "END", "CHECK", "INIT", and "UNITCHECK", it is easy to mistakenly add a colon to the end of the block name. E.g.: # a BEGIN block that gets executed at compile time. BEGIN { <...code...> } # an ordinary labeled block that gets executed at run time. BEGIN: { <...code...> } The labels "BEGIN:", "END:", etc. are probably errors. This policy prohibits the special Perl block names from being used as labels. CONFIGURATION
This Policy is not configurable except for the standard options. SEE ALSO
The Perl Buzz article on this issue at <http://perlbuzz.com/2008/05/colons-invalidate-your-begin-and-end-blocks.html>. ACKNOWLEDGMENT
Randy Lauen for identifying the problem. AUTHOR
Mike O'Regan COPYRIGHT
Copyright (c) 2008-2011 Mike O'Regan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames(3)
All times are GMT -4. The time now is 07:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy