英文:
use variable expansion failing in sed for inserting string with 4 tabs in a file
问题
团队,
我需要在文件的特定行之后的某些制表符后插入一个字符串。
奖励:如果字符串已经存在,则需要跳过插入。
cat test.txt
1
2
3
4
5
sed -i '3i\ \t\t\t\t<file branch-rate="0.0">' test.txt
在上述插入之后查看 test.txt
1
2
<file branch-rate="0.0">
3
4
5
使用变量分配
ln=5
sed -i '${ln}i\ \t\t\t\t<file branch-rate="0.0">' test.txt
sed: -e expression #1, char 4: extra characters after command
英文:
Team,
I need to insert a string at a certain line in a file after certain tabs.
bonus: I also need to skip insertion if it already exists.
cat test.txt
1
2
3
4
5
sed -i '3i\ \t\t\t\t<file branch-rate="0.0">' test.txt
cat test.txt < after above insertion.
1
2
<file branch-rate="0.0">
3
4
5
using variable assignment
ln=5
sed -i '${ln}i\ \t\t\t\t<file branch-rate="0.0">' test.txt
sed: -e expression #1, char 4: extra characters after command
答案1
得分: 1
你需要使用双引号 "
以便变量可以展开。同时,在使用原地编辑时,创建备份文件也是一个好主意。
你可以尝试使用以下 sed
命令:
$ sed -i.bak "${ln}i\ \t\t\t\t<file branch-rate=\"0.0\">" test.txt
英文:
You need to use double quotes "
so your variables can expand. It is also a good idea to create a backup file while using in-place editing.
You can try this sed
$ sed -i.bak "${ln}i\ \t\t\t\t<file branch-rate=\"0.0\">" test.txt
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论