bashgrepfind

(standard input) shown instead file name with grep -iHn "ERROR" and a second grep -vH "filter" shows (standard input) and no longer the file name


What I try to do:

The Problem, remove all resulting rows containing "34" in the Line

[tom@centos7-home-dell ~]$ find /home/tom/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR 
/home/tom/tmp/test_grep/file1.log:ERROR:12;34
/home/tom/tmp/test_grep/file1.log:ERROR:12
/home/tom/tmp/test_grep/file2.log:ERROR:67890
/home/tom/tmp/test_grep/file2.log:ERROR:12;34
/home/tom/tmp/test_grep/file2.log:ERROR:12
[tom@centos7-home-dell ~]$ find /home/tom/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR | grep -vH "34"
(Standardeingabe):/home/tom/tmp/test_grep/file1.log:ERROR:12
(Standardeingabe):/home/tom/tmp/test_grep/file2.log:ERROR:67890
(Standardeingabe):/home/tom/tmp/test_grep/file2.log:ERROR:12

My Solution shows expected result but I'm not shure it is o.k. because I use an xargs -0 command. Pleas have a look here:

[thor@centos7-home-dell wildfly]$ find /home/thor/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR
/home/thor/tmp/test_grep/file1.log:ERROR:12;34
/home/thor/tmp/test_grep/file1.log:ERROR:12
/home/thor/tmp/test_grep/file2.log:ERROR:67890
/home/thor/tmp/test_grep/file2.log:ERROR:12;34
/home/thor/tmp/test_grep/file2.log:ERROR:12

[thor@centos7-home-dell wildfly]$ find /home/thor/tmp/test_grep -name "*.log" -type f | xargs grep -i ERROR | xargs -0 | grep -v 34
/home/thor/tmp/test_grep/file1.log:ERROR:12
/home/thor/tmp/test_grep/file2.log:ERROR:67890
/home/thor/tmp/test_grep/file2.log:ERROR:12


Solution

  • I think all you're trying to do is this:

    find /home/tom/tmp/test_grep -name '*.log' -type f -exec \
    awk -v OFS=':' 'toupper($0) ~ /ERROR/ && !/34/ { print FILENAME, $0 }' {} +
    

    Your existing code, and so the logic copied above, would exclude a line that has a number with 34 contained inside it such as:

    /home/thor/tmp/test_grep/file2.log:ERROR:67340
    

    which I suspect isn't what you really want to happen. Given that, you may want to change the /34/ regexp to /[:;]34(;|$)/ or similar to avoid false matches, depending on what you're really trying to do.