How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern.

Example:
text1.txt:

ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB
DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF

text2.txt:

XXXX,ABCD 1234567,WORLD_HELLOA,WORLD_HELLOB
XXXX,DCBA 3456789,WORLD_HELLOA,WORLD_HELLOB

output.txt:

XXXX,ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB
XXXX,DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF

Any help will be greatly appreciated :slight_smile:

awk -F',' 'NR==FNR {a[$1]=$2","$3; next} a[$2] {print $1","$2","a[$2]}' text1.txt text2.txt

Thanks !

any idea why it is causing a syntax error?

awk -F, 'NR==FNR{A[$1]=$0;next}A[$2]{print $1 FS A[$2]}' file1 file2

Thanks pamu!

But same as the previous reply it has syntax error. Sorry.. I'm kinda new with shell scripting. Any guidance will be appreciated :slight_smile:

have you tried with nawk ?

Nice ! I got what i wanted. Thank pamu and balajesuri. You guys are good ! :smiley:

---------- Post updated at 02:13 AM ---------- Previous update was at 01:57 AM ----------

Pamu, do you mind to do some explanation on the code ? Thank you :smiley:

awk -F, 'NR==FNR{     #Reading file1

A[$1]=$0;next}             #Assign $0 to array A with index $1. Using next it skips the next code.

A[$2]            # Check If the A[$2] is present or not. Here we are reading file2

{print $1 FS A[$2]}'       # print $1 and A[$2](values from file1) with Field separator.

Hope this helps you.

Regards,
pamu

text1.txt

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

text2.txt

ABCD 123456,1111,11111,111111,XX,AAAAAA1,BBBBBB1
CDEF 123456,1111,11111,111111,XX,AAAAAA2,BBBBBB2
BCDF 123456,1111,11111,111111,XX,AAAAAA3,BBBBBB3

Output:

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,AAAAAA3,BBBBBB2

With some edit on the code above i still cant get the result i wanted. Any one know how do i solve this ? Thank you.

There is huge difference between your previous input file and current input file.
Try

awk -F, 'NR==FNR{A[$1]=$NF;B[$NF]=$(NF-1);next}{$(NF-1)=A[$3];$(NF-2)=B[$(NF-1)]}1' OFS="," file2 file1

It gave me the wrong output.
output:

XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345

Please check my previous post.
I have corrected the code.

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,AAAAAA3,BBBBBB2

Thank pamu! It solved my problem ! What if i want to remove the ",XX" from the output ? May i check what do i have to do to obtain the above output?

do you want to remove only XX or all X .
Please provide me your desired output.

You can use awk's inbuilt sub functionality to remove X .

Regards,
pamu

text1.txt

Code:

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

text2.txt

Code:

ABCD 123456,1111,11111,111111,XX,AAAAAA1,BBBBBB1
CDEF 123456,1111,11111,111111,XX,AAAAAA2,BBBBBB2
BCDF 123456,1111,11111,111111,XX,AAAAAA3,BBBBBB3

Output:

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

I just want to remove the highlighted ",XX". :slight_smile:

---------- Post updated 06-14-13 at 02:56 AM ---------- Previous update was 06-13-13 at 07:35 PM ----------

Tried with this command to solve the above problem but i am encountering syntax error. Any suggestion ?

awk {'sub("/,1/","");'} final.txt

In your code ' position is wrong it should be like

awk '{sub()}' final.txt

try

awk -F, 'NR==FNR{A[$1]=$NF;B[$NF]=$(NF-1);next}{$(NF-1)=A[$3];$(NF-2)=B[$(NF-1)];sub(",XX,",",")}1' OFS="," file2 file1

Thank pamu ! It solved my problem ! Millions of thanks !

Desired Output:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,

Output:
,XX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,YYYYY1,ZZZZZ1,

The output initially is correct however when i change an input file with same format. The first character is being cut and replaced with ",". Any help?

The code:

awk -F, 'NR==FNR{A[$1]=$NF;B[$NF]=$(NF-1);next}{$(NF-1)=A[$3];$(NF-2)=B[$(NF-1)];sub(",XX,",",")}1' OFS="," file2 file1

Can you please provide your changed input file

Regards,

pamu

New input file:
XXX XXXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,,XX,YYYYY1,ZZZZZ1,

Sorry that i forgot to put post the new input file. it look somehow similar to the previous input file.