首页 > 解决方案 > 字符串 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 

标签: bashshellawksedgrep

解决方案


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

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

以上将在任何 UNIX 机器上的任何 shell 中的任何 awk 上稳健有效地工作,并且如果/当您的需求稍后发生变化时,增强/维护将是微不足道的。


推荐阅读