update problem

Hello Friends, I am completely new to unix. Joined forum today. I have a problem which may seem very simple to you all. I will really appreciate your help. :slight_smile:

I have a file.

Purpose: to update this file as new product comes in. (product means: acer,intel, amd, etc..)

CASE 1: New Product: acer,41,Q-8033762700,ok
(you can consider this 4 fields seperated by comma as input arguments)
i.e. we have new product (acer41 instead 40) as below, then we need to add new line

output file should look like

CASE 2: New Product: dell,22,Q-8098763501,ok
i.e. we have same product (dell 22 but Q change from Q-8098763500 to Q-8098763501) as below,then we need to replace that line

output file should look like

Brief: This 2 case should keep working. lines will keep getting added when new product is there. & if same product & Q change then line will be replaced.

I will be really thankful if someone helps as soon as poss. :b:

Without any validation or error handling:

echo "dell,22,Q-8098763501,ok" >> file
echo "acer,41,Q-8033762700,ok" >> file
awk -F, '{arr[$1$2] = $0} END { for (i in arr) { print arr }}' file > newfile
mv newfile file

Thank you very much admin. Perfect solution. :slight_smile: