I have to filter lines like the following:

[javac] /Users/looris/Sviluppo/android/projects/toutry/src/net/looris/toutry/Stuff.java:23: warning: unmappable character for encoding ascii
[javac]             return (poked=false); // NOTA: è un'assegnazione, non un controllo!
[javac]                                                ^

I've tried |grep -v -A2 "unmappable character for encoding ascii" but it just does nothing.

If I just do |grep -v "unmappable character for encoding ascii" it does filter that line, but I need to filter the following two lines too.

(using "grep (GNU grep) 2.5.1" under OSX 10.5)

link|improve this question

80% accept rate
I don't think you can mix -v and -A but You should be able to use sed or awk instead. – Nifle Apr 17 '10 at 15:52
feedback

1 Answer

up vote 2 down vote accepted

If you call grep -A2 -v, it will start to skip lines if there are more then 2 lines righ after each other that contains the search pattern, which is obviously not what you want. Try this:

| awk 'BEGIN { skip = 0 } /unmappable character for encoding ascii/ { skip = 3 } { if (skip > 0) { skip-- } else { print $0 } }'

The AWK code expanded:

BEGIN { 
    skip = 0
}
/unmappable character for encoding ascii/ { 
    skip = 3
}
{ 
    if (skip > 0) { 
        skip--
    } else { 
        print $0
    } 
}
link|improve this answer
awesome, thanks! – Lohoris Apr 17 '10 at 20:04
unfortunatly this method has the problem of killing the return value. Any solution? – Lohoris Apr 18 '10 at 15:06
well, a workaround: if ($COMPILE) { $CE=xsys('ant debug>/tmp/antdebug.out'); xsys('cat /tmp/antdebug.out| awk \'BEGIN { skip = 0 } /unmappable character for encoding ascii/ { skip = 3 } { if (skip > 0) { skip-- } else { print $0 } }\''); $CE and diex("Compile error"); } – Lohoris Apr 18 '10 at 15:11
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.