Help with awk solution to add columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with awk solution to add columns
# 1  
Old 11-12-2014
Help with awk solution to add columns

Hi all. Wondering if someone can help with an awk solution to a problem I'm stumped with.

I have a matrix file with >1000 fields and would like to add another column after each column with a text label. For example:

Input:
Code:
$cat file.txt
name     col1     col2     col3     coln
aaaa     2323     2132     2343     6456
bbbb     4234     3243     4342     4324
cccc     3424     4342     6546     6546
dddd     3213     3213     3213     3123
eeee     3123     5453     2453     7653

Desired output:
Code:
$cat result.txt
name     col1     col1   col2     col2   col3     col3   coln     coln
aaaa     2323     AB     2132     AB     2343     AB     6456     AB
bbbb     4234     AB     3243     AB     4342     AB     4324     AB
cccc     3424     AB     4342     AB     6546     AB     6546     AB
dddd     3213     AB     3213     AB     3213     AB     3123     AB
eeee     3123     AB     5453     AB     2453     AB     7653     AB

I appreciate the help greatly.

Torch
# 2  
Old 11-12-2014
Here is an awk approach:
Code:
awk '
        NR == 1 {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i > 1 )
                                printf $i "\t" ( i == NF ? $i RS : $i "\t" )
                        else
                                printf $i "\t"
                }
        }
        NR > 1 {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i > 1 )
                                printf $i "\t" ( i == NF ? "AB" RS : "AB" "\t" )
                        else
                                printf $i "\t"
                }
        }
' file.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-12-2014
That appears to have done the trick!

Thanks so much!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

2. Shell Programming and Scripting

Add 8 columns at the end of .csv file using awk

Hello all, I have a .csv file of 16 columns consists of bunch of numbers. 6.45E+01 1.17E+01 8.10E+04 8.21E+01 8.50E+00 1.20E+01 1.02E+01 1.88E+01 1.86E+04 3.53E+03 1.09E+07 3.82E+04 2.09E+03 3.57E+03 2.98E+03 3.93E+03 6.34E+01 3.23E+01 9.24E+04 ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

3. UNIX for Dummies Questions & Answers

awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ---------- For some reason my question is not getting printed. Here are the details: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

4. UNIX for Dummies Questions & Answers

Help with solution to add together columns of large file

Hi everyone. I have a file with ~500 columns and I would like to perform a simple calculation on every two columns. The file looks like this: $cat input id A B C D E F.....X 1 2 4 2 3 4 1 n 2 4 6 4 6 4 5 n 3 4 7 5 2 2 3 n 4 ... (5 Replies)
Discussion started by: torchij
5 Replies

5. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

6. Shell Programming and Scripting

How to add columns based on a pattern using awk?

Hi, I have a file with more than 1000 lines with ~14 columns. I need to find all the lines with matching value in column 14 and then add column 6 in all the lines before printing them out.. e.g if this is the input file: abc test input 10 for process 2345 abc test input 15 for process 2348... (1 Reply)
Discussion started by: xkdasari
1 Replies

7. Shell Programming and Scripting

Any solution with awk for volatile columns??

Hi I have this file with content ale,4 ,ale,2 ,ale,1 ,ale,2 ale,1 ,ale,7 ,ale,7 ,ale,13 ale,6 ,ale,1 ,ale,1 ,ale,1 ale,1 ,ale,1 ,ale,37 ,ale,1 ale,1 ,ale,1 ,ale,2 ,ale,37 ale,77 ,ale,1 ,ale,53 ,ale,3 ale,5 ,ale,1 ,ale,2 ,ale,40 ale,1 ,ale,1 ,ale,44 ,ale,1... (7 Replies)
Discussion started by: nikhil jain
7 Replies

8. Shell Programming and Scripting

AWK : Add columns in the end of csv file

Hi everybody, I need some help please I have a csv file named masterFile1.csv header1,header2,header3 value1,value2,value3 value4,value5,value6 I am trying to add new columns in the end of the csv to have a new csv file named masterFile2.csv like this :... (3 Replies)
Discussion started by: villebonnais
3 Replies

9. Shell Programming and Scripting

AWK solution to subtract multiple columns of numbers

Hope somebody is happy. NR==1 { num_columns=split( $0, menuend ); next; } { split( $0, substrend ); for ( i=1; i<=NF; i++ ) { minuend -= substrend; } } END { print "Result:"; for ( i=1; i<=num_columns; i++ ) { printf(... (3 Replies)
Discussion started by: awkward
3 Replies

10. Shell Programming and Scripting

add columns with awk

hi everybody: My question is how could i add a new columns to a file that it has one column like this: 06/06/2005 06/07/2005 06/08/2005 06/09/2005 06/10/2005 06/11/2005 06/12/2005 06/13/2005 06/14/2005 06/15/2005 06/16/2005 06/17/2005 .... And i want add columns like : 06/06/2005 ... (4 Replies)
Discussion started by: tonet
4 Replies
Login or Register to Ask a Question