字符串grep中的Bash换行符

字符串grep中的Bash换行符,bash,shell,awk,sed,grep,Bash,Shell,Awk,Sed,Grep,如何在我的grep之后添加一个新字符串 $ cat ansible/file | grep "Other users" Other users 您可以使用sed查找要查找的字符串,并将其替换为自身+新行: $ cat ansible/file | grep "Other users" Other users NewString # New line with new string appended to file 我们将“其他用户”替换为“其他用户\n新字符串”(这是字符串本身,并在其后

如何在我的grep之后添加一个新字符串

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

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

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

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

sed用于在单个行上执行s/old/new/操作,仅此而已。对于其他内容,您应使用awk:

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

上述内容将在任何UNIX设备上的任何shell中的任何awk中都能可靠而高效地工作,并且在您的需求以后发生变化时,对其进行增强/维护将是微不足道的。

建议和观察:1)除非需要双引号,否则使用单引号2)如果匹配行中有其他字符,您需要说明3)
\n
不可移植4)sed有一个用于此用例的
命令使用此命令:cat ansible/file | grep“Other users”| | awk'{print$0”\nNewString}
awk '{print} /Other users/{print "New Line"}' file