使用两个文件来查找/替换第三个文件[解决] [英] Using two files to search/replace a third file [solved]
问题描述
我有两个文件:结果
correct.txt
天是蓝的结果
我喜欢吃苹果结果
。结果
the sky is blue
I like eat apple
.
.
和 wrong.txt
天空是蓝色的结果
我喜欢吃苹果结果
。结果
。结果
the sky are blue
I like eat apple
.
.
.
有很多这两个文件的行。
现在,我想用我的搜索中的wrong.txt更正第三个文件
使用correct.txt对其进行校正。
There are a lot of lines in both files. Now, I want to correct a third file using my search in the "wrong.txt" to correct it using the "correct.txt".
我已经创建了两个文件:
I have created two files:
readarray -t correct_array < correct.txt
readarray -t wrong_array < wrong.txt
要纠正该文件的 to_be_corrected.txt
本作品:
for c in "${correct_array[@]}"
do
echo "$c"
done
这个我试过
for e in "${correct_array[@]}"
do
sed -i.bak 's/$wrong_array[@]/$correct_array[@]/' to_be_corrected.txt
done
但是,这并不工作。结果
如何使用 SED
使用数组?
推荐答案
您正在使用单引号(
)为您的 SED
命令,所以外壳并不评估变量 $ wrong_array [@]
和 $ correct_array [@]
。尝试在变量双引号和括号内。此外,您使用的是整个阵列 $ {correct_array [@]}
。你需要的元素配对在一起,或许与索引:
You are using single quotes ('
) for your sed
command, so the shell is not evaluating the variables $wrong_array[@]
and $correct_array[@]
. Try double quotes and braces on the variables. Also, you are using the entire array with ${correct_array[@]}
. You need to pair the elements together, perhaps with an index:
for ((e=0; e<"${#correct_array[@]}"; ++e)); do
sed -i.bak "s/${wrong_array[$e]}/${correct_array[$e]}/" to_be_corrected.txt
done
这个迭代电子
在数组的索引( $ {#correct_array [@]}
给出大小数组的),那么电子
用于索引 wrong_array
的相应元素和 correct_array
。希望你没有任何引号(单人或双人),在你的文本文件。
This iterates e
over the indexes of the array (${#correct_array[@]}
gives the size of the array) then e
is used to index the corresponding elements of wrong_array
and correct_array
. Hopefully you don't have any quotes (single or double) in your text files.
这篇关于使用两个文件来查找/替换第三个文件[解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!