我有以下段落(保存在文件text.txt上):
Good morning, my name is Maria.
I study in the university.
I like to travel.
我想最后得到三个段落,但是有不同的女性名字。期望的输出:
Good morning, my name is Maria.
I study in the university.
I like to travel.
Good morning, my name is Lola.
I study in the university.
I like to travel.
Good morning, my name is Veronica.
I study in the university.
I like to travel.
所以我创建了一个名为names.txt的txt文件:
Maria
Lola
Veronica
我想提出一个迭代段落的for循环,但用sed替换名称。所以我做了:
for i in names.txt; do sed 's/Maria/"$i"/g' text.txt; done
但我得到的是:
Good morning, my name is "$i".
I study in the university.
I like to travel.
有人知道我的代码中有什么问题吗?谢谢!
答案 0 :(得分:1)
使用:
for i in $(< names.txt); do
sed "s/Maria\./$i\n/g" text.txt
echo # newline
done
引用问题而忘记阅读文件。这就是我用
做的事情$(< names.txt)
“双引号”每个包含空格/元字符和每个扩展的文字:"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
。使用'single quotes'
代码或文字$'s: 'Costs $5 US'
,ssh host 'echo "$HOSTNAME"'
。见
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words