字符串grep中的Bash换行符

时间:2018-07-08 11:15:01

标签: bash shell awk sed grep

$ cat ansible/file | grep "Other users"
Other users

如何在上述grep之后在新行上添加新字符串?

$ cat ansible/file | grep "Other users"
Other users
NewString  # New line with new string appended to file 

2 个答案:

答案 0 :(得分:1)

sed用于单独一行上的s / old / new /。对于其他任何事情,您都应该使用awk:

awk '{print} /Other users/{print "New Line"}' file

以上内容将与任何UNIX盒中任何shell中的任何awk一起健壮而有效地工作,并且在以后更改需求时,对于增强/维护它是微不足道的。

答案 1 :(得分:0)

您可以使用sed查找所需的字符串,并将其替换为自身+新行:

sed "s/Other users/Other users\nNew Line/g"

我们将“其他用户”替换为“其他用户\ n新字符串”(这是字符串本身,并在其中添加了要插入内容的新行)。