Add column base on same name of second column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add column base on same name of second column
# 1  
Old 04-10-2014
Add column base on same name of second column

HI Guys,


I have input file A.txt

Code:
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

I want Output base on same name in column B and that name at column C

Output.txt

Code:
 1 AAA AAA  
 1 BBB BBB  
 1 1SW BBB  
 1 2SW BBB  
 1 3SW BBB  
 1 BBB BBB  
 1 PPP PPP  
 1 1SW PPP  
 1 PPP PPP  
 1 1WS AAA  
 1 1AS AAA  
 1 P1P AAA  
 1 AAA AAA

Between Both AAA and AAA all column have AAA but in inner loop all Column have PPP Between PPP same as BBB.
# 2  
Old 04-11-2014
Code:
awk '{if($2 !~ /[0-9]/) {$3 = $2}; a[NR] = $0}
  END {for(i = NR; i >= 1; i--)
    {split(a[i], b, FS);
 if(b[3] != "") {t = b[3]};
 c[i] = (b[1] FS b[2] FS t)}; delete a;
 for(i = 1; i <= NR; i++) print c[i]}' A.txt

---------- Post updated at 02:46 AM ---------- Previous update was at 02:15 AM ----------

even this code will work
Code:
awk '{a[NR] = $0}
  END {for(i = NR; i >= 1; i--) {split(a[i], p, FS);
    if(p[2] !~ /[0-9]/) {c = p[2]};
 b[i] = (a[i] FS c); delete a[i]};
    for(i = 1; i <= NR; i++) {print b[i]}}' file

This User Gave Thanks to SriniShoo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add column and multiply its result to all elements of another column

Input file is as follows: 1 | 6 2 | 7 3 | 8 4 | 9 5 | 10 Output reuired (sum of the first column $1*$2) 1 | 6 | 90 2 | 7 | 105 3 | 8 | 120 4 |9 | 135 5 |10 | 150 Please enclose sample input, sample output, and code... (5 Replies)
Discussion started by: Sagar Singh
5 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

Sum of Columns Base on First Column

Input :- Hd1;Hd2:hd3;Hd4;Hd5 X;1;2;3;4 Y;2;3;5;6 Z;3;5;6;7 X;10;11;24;16 Y;11;23;21;1 Z;10;13;14;15 X;0;1;2;0 K;0;0;0;0 K;0;0;0;0 I want Sum Data base on first column; Hd1;Hd2:hd3;Hd4;Hd5 X;11;14;29;20 Y;12;26;26;7 Z;13;18;20;22 K;0;0;0;0 (4 Replies)
Discussion started by: pareshkp
4 Replies

4. Shell Programming and Scripting

Grep Data Base on Column

HI Want to grep data from column header and match with second file. File A.txt 1 2 3 4 5 6 X1 A L D J Q R X2 B M K P w T X3 C S L P e Y X4 R Z M A r U FileB.txt 1 2 3 4 6 7 (3 Replies)
Discussion started by: pareshkp
3 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 Replies

8. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt 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 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

9. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

10. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies
Login or Register to Ask a Question