Append column using awk/nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append column using awk/nawk
# 1  
Old 11-09-2012
Append column using awk/nawk

Is there any way I can achieve this?

Considering test1 :
Code:
a 1 2 3 4
b 2 3 4 5
c 12 1232 14 1
d 10 13 4 5
e 1 5 6 9 1

And test to be some string :
Code:
qw
twe
tew
we
qw

I want something like this :
Code:
a 1 2 qw 4
b 2 3 twe 5
c 12 1232 tew 1
d 10 13 we 5
e 1 5 qw 9 1

I am working on Solaris 10 platform, so using nawk.
I can use two nawks, but I heard nawk to be really powerful, so don't feel like using two of 'em for one purpose.Smilie
This is the code that I am using right now :

Code:
nawk '{str1=$0; getline < "test"; print str1" "$0}' test1 | nawk '$4=$NF,$NF=" "'

Moderator's Comments:
Mod Comment code tags please

Last edited by jim mcnamara; 11-09-2012 at 08:59 PM..
# 2  
Old 11-09-2012
Try this:
Code:
nawk -v f2=test '{
        if((rc = getline nf4 < f2) != 1) {
                printf("Error processing line %d from input file %s\n",
                        NR, FILENAME)
                printf("  getline < %s returned %d; expected 1: Exitting.\n",
                        f2, rc)
                exit 1
        }
        $4 = nf4
        print
}' test1

# 3  
Old 11-09-2012
also try:
Code:
awk 'NR==FNR {a[NR]=$0; next}{$(NF-1)=a[FNR]; print}' test test1

# 4  
Old 11-10-2012
Hi

Assuming your files as f1 and f2:

Code:
awk '{getline x<"f2";$NF=x FS $NF;}1' f1

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 11-10-2012
Code:
nawk 'getline $4<f' f=test test1

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-12-2012
Thank you guys...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

4. Shell Programming and Scripting

Append data with substring of nth column fields using awk

Hi guys, I have problem to append new data at the end of each line of the files where it takes whole value of the nth column. My expected result i just want to take a specific value only. This new data is based on substring of 11th, 12th 13th column that has comma seperated value. My code: awk... (4 Replies)
Discussion started by: null7
4 Replies

5. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

6. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

7. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

8. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

9. Shell Programming and Scripting

Append tabs at the end of each line in NAWK -- varying fields

Hi, I need some help in knowing how I can append tabs at the end of each line... The data looks something like this: field1, field2, field3, field4 1 2 3 4 5 I have values in field1 and field 2 in the first row and I would like to append tab on field3 and field4 for the first row..and in... (6 Replies)
Discussion started by: madhunk
6 Replies

10. Shell Programming and Scripting

append field to file(nawk)

I have two files. File A and File B. File A has two fields separated by comma 352020252365988, 652020100572356 546876543215667, 652065465654686 ... File B has many Fields separate by spaces Date Name 352020252365988 Reference Date2 Name2 546876543215667 Reference I want to... (4 Replies)
Discussion started by: axl
4 Replies
Login or Register to Ask a Question