逐行读取一个文本文件,每一行用刚刚读取的行的所有大写字符串替换另一个文本文件

问题描述 投票:0回答:1

例如 逐行读取f1.txt,对于每一行L,将f2.txt替换为L的全部大写字符串。

f1.txt

sone
stwo

f2.txt

st
 sone
  stwo
so

所需输出:

st
 SONE
  STWO
so

我尝试过:

while IFS= read -r line; do
  sed -e 's/\(.*\)/\U\1/' f2.txt
done < f1.txt
bash awk sed
1个回答
0
投票

使用 bash >4.3:

# load strings into an array
mapfile -t a <f1.txt

# load file as a string (single array element)
mapfile -d '' b <f2.txt

# do substitutions
for s in "${a[@]}"; do b=${b//"$s"/"${s^^}"}; done

# output the result
printf '%s' "$b" >new_f2.txt
© www.soinside.com 2019 - 2024. All rights reserved.