字符串 grep 中的 Bash 换行符


$ 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 

sed 用于在单独的行上执行 s/old/new/,仅此而已。对于其他任何内容,您应该使用 awk:

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

以上内容将健壮而高效地与任何 UNIX 机器上任何 shell 中的任何 awk 一起工作,并且当您的需求以后发生变化时,增强/维护将是微不足道的。

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

sed "s/Other users/Other usersnNew Line/g"
我们将"其他用户">

替换为"其他用户新字符串"(这是字符串本身,并在其后附加了要插入的内容的新行(。

最新更新