bash - 字符串 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 上稳健有效地工作,并且如果/当您的需求稍后发生变化时,增强/维护将是微不足道的。
推荐阅读
- laravel - 对工厂内的其他属性使用默认工厂属性值
- c# - C# 如果 powershell 脚本结束显示消息
- flask - 没有名为“flask_bootstrap”的模块
- android - 如何使用 AndroidXMapFragment 在活动中应用 onConfigurationChanged()
- powershell - 设置 app_offline.htm 后,如何使用 powershell 在 Asp .net 核心中获取站点状态?
- c++ - 为什么 Direct3D 11 中的顶点输入参数使用 DXGI 格式
- python - 如何在python中使用字符串连接创建变量
- mysql - 带有动态数组的 MySQLi LIKE 查询
- spring-boot - 带有 docker 文件和任务定义的 Spring-boot 配置文件最佳实践
- javascript - 每当我滚动浏览该部分时,突出显示我的导航栏菜单项?仅使用 Javascript