Replacing field from file 1 with field from file 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing field from file 1 with field from file 2
# 1  
Old 04-25-2011
Replacing field from file 1 with field from file 2

I've been searching these forums for the past 2 hours and i've found stuff that almost works but not quite.

i have file 1:
Code:
A, 1, Bla, 123
B, 2, Bla2, 123
C, 3, Bla3, 123
D, 4, Bla4, 123

file 2:
Code:
1, Aha
3, Ok

Output:
Code:
A, 1, Aha, 123
B, 2, Bla2, 123
C, 3, Ok, 123
D, 4, Bla4, 123

I thought I could do a join on file 1 and file 2, however join will insert file 2 into the first two rows, and incorrect merge the file with the -a1 flag.
I figured I could join and simply pipe it to awk to do a stupid print of only the specific columns I need, but join didn't work right for me.

I tried awk, but I've only managed to append the values to all of the rows.



Anyhelp would be appreciated,

thanks in advanced.

Last edited by joeyg; 04-25-2011 at 04:43 PM.. Reason: code tags, please!
# 2  
Old 04-25-2011
YMMV:
Code:
nawk -F, 'FNR==NR{f2[$1]=$2;next} $2+0 in f2 {$3=f2[$2+0]}1' OFS=, file2 file1

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-25-2011
Can you show what you have done so far

Rather than having someone start from scratch, please share what you have done so far. Especially if you are that close to an answer.
# 4  
Old 04-25-2011
sure,

i'm working through vgersh99's solution right now so will need to try and get it working with the actual example (so I can modify it going forward).

Code:
join -t"," -1 2 -2 1 file1.txt file2.txt

that gives me

Code:
A, 1, Bla, 123, 1, Aha
B, 2, Bla2, 123, 2, Ok
C, 3, Bla3, 123
D, 4, Bla4, 123

which is not expected.

vgersh99: thanks for the reply

can I ask what
Code:
FNR==NR{f2[$1]=$2;next} --copies the second value from file 2 into an array
$2+0 in f2 -- What does this line do?
{$3=f2[$2+0]}1' --this affects file1, if the $3 file matches your value in f2, print buffer
OFS=, file2 file1

is that right?
# 5  
Old 04-25-2011
Code:
FNR==NR{f2[$1]=$2;next} --copies the second value from file 2 into an array
# FNR==NR is true for the FIRST file to be processed 
# f2 - an array index by the value of the FIRST field with the value of the SECOND field (from file2)

$2+0 in f2 -- What does this line do?
# We get here we already processed file2 - here we're dealing with file1
# $2+0 - the value of the SECOND field. As it seems that you have a leading spaces in file 1,
#        by adding '0' we convert a field value to an int
# $2+0 in f2 - the value of the second field of file1 is in array 'f2'
#              '$2+0' is one of the indexes (not values) of array f2 

#
{$3=f2[$2+0]}1' --this affects file1, if the $3 file matches your value in f2, print buffer
# assign the the value from array f2 indexed by '$2+0' to the value of the THIRD field.
OFS=, file2 file1

# 6  
Old 04-25-2011
Quote:
Originally Posted by vgersh99
Code:
FNR==NR{f2[$1]=$2;next} --copies the second value from file 2 into an array
# FNR==NR is true for the FIRST file to be processed 
# f2 - an array index by the value of the FIRST field with the value of the SECOND field (from file2)

$2+0 in f2 -- What does this line do?
# We get here we already processed file2 - here we're dealing with file1
# $2+0 - the value of the SECOND field. As it seems that you have a leading spaces in file 1,
#        by adding '0' we convert a field value to an int
# $2+0 in f2 - the value of the second field of file1 is in array 'f2'
#              '$2+0' is one of the indexes (not values) of array f2 

#
{$3=f2[$2+0]}1' --this affects file1, if the $3 file matches your value in f2, print buffer
# assign the the value from array f2 indexed by '$2+0' to the value of the THIRD field.
OFS=, file2 file1

mm what if i wanted the second field to match, but replace the 3rd field?
# 7  
Old 04-25-2011
Quote:
Originally Posted by jghi123
mm what if i wanted the second field to match, but replace the 3rd field?
This is what the code does now!
Unless I'm misunderstanding your question.
Post sample data and the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing field based on the value of other field

Hi Team, In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version.... (14 Replies)
Discussion started by: weknowd
14 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

4. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

5. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

6. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

7. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

8. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

9. Shell Programming and Scripting

replacing field in specific line in a file

Hi, I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's... (5 Replies)
Discussion started by: vytenis
5 Replies

10. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies
Login or Register to Ask a Question