grep -v -A not working properly (actually not working at all)

1

0

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)

o0'.

Posted 2010-04-17T14:45:37.597

Reputation: 1 868

I don't think you can mix -v and -A but You should be able to use sed or awk instead. – Nifle – 2010-04-17T15:52:36.713

Answers

2

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
    } 
}

petersohn

Posted 2010-04-17T14:45:37.597

Reputation: 2 554

unfortunatly this method has the problem of killing the return value. Any solution? – o0'. – 2010-04-18T15:06:26.660

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"); } – o0'. – 2010-04-18T15:11:04.840