Bashing of Fields then append!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bashing of Fields then append!
# 1  
Old 12-06-2013
Bashing of Fields then append!

Hi All,

I;m a newbie here, Seeking for your assistance regarding on how to bash the 6th and 7th fields of file2 into 2th and 3rd fields of file1 then add fields in the last line of file2 if it's match, if not they will only display the records.

Ex.
Code:
File1
a,1,2,b,c,d,e,USA
q,3,4,w,e,r,t,CAN

File2
t,y,u,i,o,1,2,5
v,b,n,m,h,3,4,9
a,s,d,f,g,h,j,7

Expected Output:
Code:
t,y,u,i,o,1,2,5,USA - since the 6th and 7th field of file2 is = to 2th and 3rd of file1 it append the last field USA
v,b,n,m,h,3,4,9,CAN - since the 6th and 7th field of file2 is = to 2th and 3rd of file1 it append the last field CAN

a,s,d,f,g,h,j,7 - the 6th and 7th field of file2 is NOT EQUAL to 2th and 3rd of file1 it will only retain the display of records.

Please advise,

Thanks,
# 2  
Old 12-06-2013
Is this a homework assignment?
# 3  
Old 12-06-2013
Nope. Pls see code below that I try but I dont know else what to do.
Code:
Awk -F, 'FNR==NR {A [$2 FS $3]=$7; NEXT}($6 FS $7 in A )

.

Pls advise

Thanks

Last edited by Don Cragun; 12-06-2013 at 03:02 AM.. Reason: Add CODE tags.
# 4  
Old 12-06-2013
Quote:
Originally Posted by nikka
Nope. Pls see code below that I try but I dont know else what to do.
Code:
Awk -F, 'FNR==NR {A [$2 FS $3]=$7; NEXT}($6 FS $7 in A )

.

Pls advise

Thanks
Case matters in awk and shell programming; in some cases, so does spacing, and quotes have to come in matched pairs. Try:
Code:
awk -F, 'FNR==NR {A[$2 FS $3]=$7; next}(($6 FS $7) in A)'

Of course, you also have to give this command at least two files to work on. You haven't specified any input files in the command you showed us.
# 5  
Old 12-06-2013
Thanks and Noted Sir Don. I'm Sorry i'm newbie in unix. i'll try harder next tym. may i know on how to retain the records if it's not equal?

Please advise,

Thanks,
# 6  
Old 12-06-2013
Quote:
Originally Posted by nikka
Thanks and Noted Sir Don. I'm Sorry i'm newbie in unix. i'll try harder next tym. may i know on how to retain the records if it's not equal?

Please advise,

Thanks,
Sorry, I just fixed the syntax errors without checking whether or not your script did what you said you wanted. Try:
Code:
awk -F, '
FNR == NR {
        A[$2 FS $3] = $8
        next
}       
{       x = $6 FS $7
        if(x in A) $9 = A[x]
        print
}' OFS=, File1 File2

which produces the output:
Code:
t,y,u,i,o,1,2,5,USA
v,b,n,m,h,3,4,9,CAN
a,s,d,f,g,h,j,7

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching two fields in two csv files, create new file and append match

I am trying to parse two csv files and make a match in one column then print the entire file to a new file and append an additional column that gives description from the match to the new file. If a match is not made, I would like to add "NA" to the end of the file Command that Ive been using... (6 Replies)
Discussion started by: dis0wned
6 Replies

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

3. Shell Programming and Scripting

Bashing of 2 files

Hi All, Seeking for your assistance on how to bash 2 files and then print if the condition met. Ex. file1.txt: Field1 Field2 Field 3 Field 4 usa <blank> <blank> INDIA file2.txt: Field1 Field2 Field 3 Field 4 canada jap INDIA utah... (7 Replies)
Discussion started by: znesotomayor
7 Replies

4. Shell Programming and Scripting

Bashing of 2 Files

Hi Everyone, Seeking for your assistance on how to bash the file 1 to file 2 records and output the same records with specific field to display to file3. Example: Imagine we have many Files and records in File 1 and the File2 is only 1 file but many records. File1... (3 Replies)
Discussion started by: znesotomayor
3 Replies

5. UNIX for Dummies Questions & Answers

Bashing of records in different file

Hi Everyone, Good Day, I'm a newbie on scripting, I would like to seek you for assistance regarding on how to bash the 1st file into 2nd file and output the match record. Sample: File1.csv JuanDelaCrus<tab>USA<tab>CANADA TwoDelaCrus<tab>SG<tab>California File2.csv... (2 Replies)
Discussion started by: znesotomayor
2 Replies

6. Shell Programming and Scripting

UNIX append field with comparing fields from multiple column

I have a csv dump from sql server that needs to be converted so it can be feed to another program. I already sorted on field 1 but there are multiple columns with same field 1 where it needs to be compared against and if it is same then append field 5. i.e from ANG SJ,0,B,LC22,LC22(0) BAT... (2 Replies)
Discussion started by: nike27
2 Replies

7. UNIX for Dummies Questions & Answers

Match values from 2 files and append certain fields

Hi, I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly. ... (1 Reply)
Discussion started by: redse171
1 Replies

8. Shell Programming and Scripting

Match data based on two fields, and append to a line

I need to write a program to do something like a 'vlookup' in excel. I want to match data from file2 based on two fields (where both match) in file1, and for matching lines, add the data from two of the fields from file2 to file1. If anyone knows something in perl or awk that can do this, I'd be... (20 Replies)
Discussion started by: jamessmith01
20 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. UNIX for Dummies Questions & Answers

All right. ... (no Microsoft bashing, thanks)

First off, this is as much as I know about Unix: its an operating system. Now, I know it goes a tad deeper than that, so first off, what exactly is Unix? Next, im freakin sick of everything microsoft crashing on me, so Id like to make Unix my OS, but I dont want to lose anything but internet... (5 Replies)
Discussion started by: gc_wyo_dave
5 Replies
Login or Register to Ask a Question