linux 中awk命令实现字符串的精确替换
001、
root@DESKTOP-1N42TVH:/home/test3# ls a.txt test.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt ## 测试数据, 1、2列分别为替换字符和被替换字符 1 AKCR02000001 2 AKCR02000001.1 3 AKCR02000001.2 root@DESKTOP-1N42TVH:/home/test3# cat test.txt ## 测试数据 AKCR02000001 df AKCR02000001 df AKCR02000001 er AKCR02000001.1 dg AKCR02000001.1 der AKCR02000001.1 fg AKCR02000001.2 ee AKCR02000001.2 ss AKCR02000001.2 aa root@DESKTOP-1N42TVH:/home/test3# cat a.txt | while read {i,j}; do awk -v a=$i -v b=$j '{j = 0; for(i = 1; i <= NF; i++) if($i == b) {j++; $i = a} if(j != 0) {print $0}}' test.txt >> result.txt; done root@DESKTOP-1N42TVH:/home/test3# ls a.txt result.txt test.txt root@DESKTOP-1N42TVH:/home/test3# cat result.txt ## 结果文件 1 df 1 df 1 er 2 dg 2 der 2 fg 3 ee 3 ss 3 aa