bash 带有 sed 的 grep 管道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4517662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:09:38  来源:igfitidea点击:

grep pipe with sed

bashsedgrep

提问by 123Ex

This is my bash command

这是我的 bash 命令

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

What I'm trying to do here is,I'm trying to iterate through sub folders and check what files contains "System.out.print" (using grep) using 2nd grep trying to get file names and line numbers using sed command I display those to console. from here I want to remove "System.out.print" with "XXXXX" how I can pipe sed command to this? pls help me thanxx

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

回答by

GNU sed has an option to change files in place:

GNU sed 有一个选项来更改文件:

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

Btw, your script could be written as:

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

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

回答by John Q

I'm just building on hop's answer, which I found to be more useful than find -exec. I had search_textdispersed all over my computer, in logs, config files and so on, but I didn't want to search (or especially change) anything in /dev, /sys, /proc, and so on. One note, read man xargs; it doesn't like file names with spaces.

我只是建立在 hop 的答案上,我发现它比find -exec. 我将search_text散布在我的计算机上、日志、配置文件等中,但我不想在 /dev、/sys、/proc 等中搜索(或特别是更改)任何内容。一注,阅读 man xargs;它不喜欢带空格的文件名。

grep -HriIl --exclude-dir=dev --exclude-dir=proc --exclude-dir=sys search_text / | xargs sed -i 's/search_text/replace_text/g'