AWK: merge two files and replace some fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: merge two files and replace some fields
# 1  
Old 03-31-2011
Question AWK: merge two files and replace some fields

Need some code tweak:

Code:
awk 'END {
for (i=1; i<=n; i++)
  if (f2[_[i]])
    print f2[_[i]]
}
NR == FNR {
f2[_[n = FNR] = $1] = $0
next
}
$1 in f2 { 
delete f2[$1]
}1' FS=, OFS=, 2.csv 1.csv > 3.csv

file 1.csv have:

$1,$2,$3,$4,$5,$6,$7,$8,$9......

file 2.csv have:

$1,$2,$3,$4,$5,$6

files merges fine, but I need replace file 1.csv values of fields $4,$5,$6 with values $4,$5,$6 from file 2.csv, other fields from $7... must be untouched.

Last edited by u10; 04-01-2011 at 06:40 AM..
# 2  
Old 03-31-2011
Code:
awk 'NR==FNR{a[$1]=$4;b[$1]=$5;c[$1]=$6;next} $1 in a {$4=a[$1];$5=b[$1];$6=c[$1]}1' FS=, OFS=, 2.csv 1.csv

# 3  
Old 03-31-2011
Thanks, but not work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Merge two text files by two fields and mixed output

Hello, I'm back again looking for your precious help- This time I need to merge two text files with matching two fields, output only common records with mixed output. Let's look at the example: FILE1 56153;AAA0708;3;TEST1TEST1; 89014;BBB0708;3;TEST2TEST2; 89014;BBB0708;4;TEST3TEST3; ... (7 Replies)
Discussion started by: emare
7 Replies

2. Shell Programming and Scripting

How to merge fields in a single file using awk ?

Hi, From a file, using: awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' i printed out the required output. But i'm trying to merge the columns. Please look at the desired output. Any suggestions? Thanks Output: 00015 PSA1 ... (5 Replies)
Discussion started by: web2moha
5 Replies

3. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

4. Shell Programming and Scripting

Need to merge multiple text files vertically and place comma between fields

Hello expert friends, I'm writing a script to capture stats using sar and stuck up at report generation. I have around 10 files in a directory and need to merge them all vertically based on the time value of first column (output file should have only one time value) and insert comma after... (6 Replies)
Discussion started by: prvnrk
6 Replies

5. Shell Programming and Scripting

Find fields and replace using awk

Code: Using ksh Var1=`awk -F";" {print $1}' Input2.txt` cat Input1.txt | awk -F";" '{$3="Var1"}' > Output.txt (13 Replies)
Discussion started by: Roozo
13 Replies

6. Shell Programming and Scripting

How to merge two or more fields from two different files where there is non matching column?

Hi, Please excuse for often requesting queries and making R&D, I am trying to work out a possibility where i have two files field separated by pipe and another file containing only one field where there is no matching columns, Could you please advise how to merge two files. $more... (3 Replies)
Discussion started by: karthikram
3 Replies

7. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

8. Shell Programming and Scripting

use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this count,1977,1978,1979 usa, , , blue japan, red, yellow,green india, , yellow,blue china, blue, yellow, green I want the output to be(replace everything, including empty data, with the most recent data): ... (1 Reply)
Discussion started by: grossgermany
1 Replies

9. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

10. Shell Programming and Scripting

AWK Merge Fields for Print Output

I've got a file with each record on a separate line and each record contains 34 fields separated by a colon and i'm trying to re-arrange the order of the fields and merge together certain fields separated by a slash (like field7/field28). I tried using an awk print statement like awk -F: 'BEGIN... (5 Replies)
Discussion started by: RacerX
5 Replies
Login or Register to Ask a Question