I have a huge file to parse & need to search and replace text but in specific fields, sharing a small sample for reference named dest. first row is header for reference.
cat dest
ID|NAME|COMPANY|NUMBER
1001|Adam||15001
1002|eve|adam&eve|15002
1003|||
1004|||50000
1005|||50001
I have separate files that contain pattern to match, text-to-be-replaced & text-to-replace
cat src
1003||15003
1004|50000|15004
1005|50001|15005
so I can run a given below while loop with sed to achieve for last 2 rows from src file.
cat src | while IFS=$'|'; read id old new; do sed -i "/^${id}/s/${old}/${new}/" dest; done
but for ID=1003
I get empty string for $old
, and it will replace all empty columns in dest file for that ID. Which I want to avoid. What I want to do is replace only last field.
DESIRED:
ID|NAME|COMPANY|NUMBER
1001|Adam||15001
1002|eve|adam&eve|15002
1003|||15003
1004|||15004
1005|||15005
I could go with awk as it is more granular, when comes to columnar data. but awk will print out multiple times to stdout, as much as I understand, which is not practical either in my case.
so is there a way that I can do it in smart & concise way?