1

I have two pipe delimited files

File1.txt

f1|f2|f3|f4|
123456|C|aws|zip|
589445|D|csv|zip|
789466|C|txt|tar|
874512|A|row|war|

File2.txt

f1|f2|f3|f4|
458788|C|aws|zip|
589445|D|||
789466|C|wd|rar|
458745|A|xls|rar|

Final.txt

123456|C|aws|zip|
789466|C|wd|rar|
874512|A|row|war|
458745|A|xls|rar|

1.Take the field f2 from File2, if it is D, take corresponding field f1 from File2 and identify the corresponding row matching to the field f1 in File1 and delete the row from File1. Same way

2.Take the field f2 from File2, if it is C, take corresponding field f1 from File2 and identify the corresponding row matching to the field f1 in File1 and replace the row in File1 with corresponding line in File2.

2.Take the field f2 from File2, if it is A, directly add the row to the File1 as new row.

  • Any awk solution – Juhan Jul 31 '18 at 13:40
1

This works for me:

while read line   
do   
i=$(echo $line | cut -d\| -f1 )  
f=$(echo $line | cut -d\| -f2 )  
case $f in  
    D) sed -i /^$i/d File1.txt ;;  
    C) sed -i s/^$i.*/$line/ File1.txt ;;  
    A) echo $line >> File1.txt;;  
esac  
done < File2.txt  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.