Change column to row base on column 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change column to row base on column 2
# 1  
Old 11-05-2012
Change column to row base on column 2

Hi Guys,

Input.txt

Code:
L194 A -118.2             
L194 B -115.1             
L194 C -118.7             
L196 A 0                  
L196 C 0                  
L197 A -111.2             
L197 B -118.9             
L197 C -119.9             
L199 A -120.4             
L199 B -119.9             
L199 C -120.3             
L207 A -120.5             
L207 B -119.0             
L207 C -118.9             
L212 A -118.5             
L212 B -119.9             
L212 C -119.5

Output.txt

Code:
No.   A      B        C 
L194 -118.2 -115.1 -118.7 
L196 0      n/a       0   
L197 -111.2 -118.9 -119.9 
L199 -120.4 -119.9 -120.3 
L207 -120.5 -119.0 -118.9 
L212 -118.5 -119.9 -119.5

Thanks
# 2  
Old 11-05-2012
Code:
[user@home-pc ~]$ cat file
L194 A -118.2
L194 B -115.1
L194 C -118.7
L196 A 0
L196 C 0
L197 A -111.2
L197 B -118.9
L197 C -119.9
L199 A -120.4
L199 B -119.9
L199 C -120.3
L207 A -120.5
L207 B -119.0
L207 C -118.9
L212 A -118.5
L212 B -119.9
L212 C -119.5 
[user@home-pc ~]$ perl -ane 'BEGIN { printf "%-10s%-10s%-10s%-10s\n", "No.", "A", "B", "C" }
$x{$F[0]}{$F[1]} = $F[2];
END {
    for (sort keys %x) {
        ! defined $x{$_}{A} && ($x{$_}{A} = "n/a");
        ! defined $x{$_}{B} && ($x{$_}{B} = "n/a");
        ! defined $x{$_}{B} && ($x{$_}{C} = "n/a");
        printf "%-10s%-10s%-10s%-10s\n", $_, $x{$_}{A}, $x{$_}{B}, $x{$_}{C};
    }
}' file
No.       A         B         C
L194      -118.2    -115.1    -118.7
L196      0         n/a       0
L197      -111.2    -118.9    -119.9
L199      -120.4    -119.9    -120.3
L207      -120.5    -119.0    -118.9
L212      -118.5    -119.9    -119.5
[user@home-pc ~]$

# 3  
Old 11-05-2012
Here is one that is not dependent on the middle column being limited from "A-C" (script can handle any items in the second column)

put this into a file called myawk.awk
Code:
NR == FNR{ c[$2]=1; next }
{ r[$1]++; l[$1,$2] = $3 }
END {
    printf("No.");  for (i in c) printf("\t%s",i); printf("\n");
    for (i in r) {
        printf("%s",i);
        for (j in c) {
            if (l[i,j]=="") { o = "n/a" } else { o = l[i,j] }
            printf("\t%s", o);
        }
        printf("\n");
    }
}

execute it by typing (and yes "file is repeated twice"):
Code:
awk -f myawk.awk file file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Column to Row base on first and second Column

Input :- Column to Row Time CT 1 2 3 17:45 X 10 15 20 18:00 X 15 20 30 18:15 X 10 10 10 18:30 X 5 5 5 17:45 Y 10 15 20 18:00 Y 15 20 30 18:15 Y 10 10 10 18:30 Y 5 5 5 output:- Time P X Y 17:45 1 10 10 18:00 1 15 15 18:15 1 10 10 (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

4. Shell Programming and Scripting

Add Column base on other Column Data

HI Guys, I want add one extra Column base on 3rd Column . Input :- M204 MS204_154 :vsDataUeMe M204 MS204_154 es:sMeasure 0 M204 MS204_154 es:90ilterCoe 9 M204 MS204_154 es:searchE9090ortTime 40 M204 MS204_154 es:servOrPrioI90HoTimer 4000 M204 MS204_154 es:ueMeajllls154545 TRUE... (5 Replies)
Discussion started by: pareshkp
5 Replies

5. Shell Programming and Scripting

Change Data base on Column

Base of last two column i want to change may data if Last two Column have A and C then Copy Column $4 to Column $3. Input :- DD142 0_1 DD142_A DD142_B A B DD142 1_1 DD142_B DD142_C B C DD142 2_1 DD142_A DD142_C A C DD142 3_1 DD142_A A DD142 3_2 DD142_A A DD142 4_1 DD142_B B ... (4 Replies)
Discussion started by: pareshkp
4 Replies

6. Shell Programming and Scripting

Add column base on same name of second column

HI Guys, I have input file A.txt 1 AAA 1 BBB 1 1SW 1 2SW 1 3SW 1 BBB 1 PPP 1 1SW 1 PPP 1 1WS 1 1AS 1 P1P 1 AAA (1 Reply)
Discussion started by: pareshkp
1 Replies

7. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

9. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

10. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies
Login or Register to Ask a Question