Home > Mobile >  Unmask data from matrix linux shell
Unmask data from matrix linux shell

Time:05-13

i have 2 file.

analizeddata.txt:

A001->A002->A003->A004
A001->A005->A007
A022->A033
[...]

and

matrix.txt:

A001|Scott
A002|Bob
A003|Mark
A004|Jane
A005|Elion
A007|Brooke
A022|Meggie
A023|Tif
[..]

How i can replace in analizeddata.txt, or obtain a new file, with the second column of matrix.txt? The expected output file will be as:

Scott->Bob->Mark->Jane
Scott->Elion->Brooke
Meggie->Tif
[...]

Thanks

CodePudding user response:

Just use sed to replace the string what you want.

  • sed 's/|/\//g' matrix.txt will generate the replace pattern likes A001/Scott which will be used as regexp/replacement of the second sed s/regexp/replacement/ command.
  • sed -i option will update directly analizeddata.txt file, back up it before exec this command.
for replace_mode in $(sed 's/|/\//g' matrix.txt); do sed -i 's/'$replace_mode'/g' analizeddata.txt; done

CodePudding user response:

Suggesting awk script:

 awk -F"|" 'FNR==NR{arr[$1]=$2;next}{for(i in arr)gsub(i,arr[i])}1' matrix.txt analizeddata.txt

with provided sample data, results:

Scott->Bob->Mark->Jane
Scott->Elion->Brooke
Meggie->A033
  • Related