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.

1
  • Any awk solution Commented Jul 31, 2018 at 13:40

1 Answer 1

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  
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.