【发布时间】:2015-09-19 15:52:36
【问题描述】:
我有一个类似的列表:
What do you want to do?
Import a text file by opening it in Excel
Import a text file by connecting to it
Export data to a text file by saving it
Change the delimiter that is used in a text file
Change the separator in all .csv text files
使用 SED 我可以在“连接”上找到匹配项并替换该行:
sed 's^.*connecting.*^Import a text file by opening it^g' crontab
这应该将上面的列表更改为:
What do you want to do?
Import a text file by opening it in Excel
Import a text file by opening it
Export data to a text file by saving it
Change the delimiter that is used in a text file
Change the separator in all .csv text files
但是我需要做的是:
如果存在包含单词connecting 的行,则替换该行,
如果该行不存在,则将其作为新行添加到列表的末尾。
我知道我可以做echo "Import a text file by opening it" >> list,这会将行添加到列表的末尾,但是无论如何我可以在一个命令中做到这一点吗?还是可以在一个实例中运行的命令?
谢谢
【问题讨论】:
-
包含“connecting”的行是否不止一行?在这种情况下会发生什么?
-
嗨 - 文件中只会出现一次“连接”。
标签: linux replace sed insert find