I have a main.txt file where I wish to replace the Sector_ID within coloumn 2 with a Sector_name, IF a mapping exist in a lookup table. Both the main.txt and the lookup.txt are tab seperated.
main.txt:
Serving_Sector Target_Sector HO_Attempts HO_Successful_Attempts
1112928 1112929 2 2
1112928 1112930 0 0
1112929 1112928 3 3
lookup.txt:
Sector_id Sector_name
1112929 SectorTEST
Any clue on how to solve this using bash? In some cases the Sector_id might not be in the lookup table. In such cases it should keep original value in main.txt
Proposed script(by @Dielna Reboot):
#!/bin/bash
#put only ids in variable
ids="$(cat hostats.txt | awk '{print $2}' | grep -v Sector)"
for sector_name in $ids; do
#match id condition
grep "$sector_name" lookup.txt >/dev/null 2>&1 && {
#save sector name
sector_id="$(grep "$sector_name" lookup.txt | awk '{print $2}')"
# replace via sed in-place
sed -i "s/$sector_name/$sector_id/g" hostats.txt
} || true
done
Result is this("->" illustrates tab):
Serving_Sector->Target_Sector->HO_Attempts_HO->Successful_Attempts
1112928->SectorTEST
->2->2
1112928->1112930->0->0
SectorTEST
->1112928->3->3
For some reason a new line is appended, and also any matches will update the coloumn 1 (Serving_sector) which in this case is not desired.
The desired result should be this("->" illustrates tab):
Serving_Sector->Target_Sector->HO_Attempts->HO_Successful_Attempts
1112928->SectorTEST->2->2
1112928->1112930->0->0
1112929->1112928->3->3