bash - 带有 sed 的 grep 管道

标签 bash sed grep

这是我的 bash 命令

grep -rl "System.out.print" Project1/ | 
    xargs -I{} grep -H -n "System.out.print" {} |
    cut -f-2 -d: |
    sed "s/\(.*\):\(.*\)/filename is \1 and line number is \2/

我在这里要做的是,我试图遍历子文件夹并检查哪些文件包含“System.out.print”(使用 grep) 使用第二个 grep 尝试获取文件名和行号 使用 sed 命令我将这些显示到控制台。 我想从这里删除带有“XXXXX”的“System.out.print”,如何将 sed 命令传递给它? 请帮助我 谢谢

最佳答案

GNU sed 有一个就地更改文件的选项:

find Project1/ -type f | xargs sed -i 's/System\.out\.print/XXXXX/g'

顺便说一句,你的脚本可以写成:

grep -rsn 'root' /etc/ |
    awk -F: '{ print "filename is", $1, "and line number is", $2 }'

关于bash - 带有 sed 的 grep 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4517662/

相关文章:

unix - 使用grep进行就地处理

bash - 删除第 4 列中包含范围模式的行

regex - 在终端中替换包含正则表达式的文本

bash - shell in()是另一个进程?

regex - 如何在匹配模式后插入字符串

csv - 从一个列表中减去另一个列表的最有效方法是什么?

linux - 大写的 Grep 语法

linux - 删除行中的空格和数字,直到行中的第一个字符

linux - 在 linux 终端中输出彩色命令后,我的命令行变色了

linux - 在Linux中安装脚本是什么意思