--- org.apache.commons.lang.text.StrBuilderTest::testIndexOfLang294
junit.framework.AssertionFailedError: expected:<-1> but was:<6>
at junit.framework.Assert.fail(Assert.java:57) # 需匹配行
at junit.framework.Assert.failNotEquals(Assert.java:329)
at junit.framework.Assert.assertEquals(Assert.java:78)
at junit.framework.Assert.assertEquals(Assert.java:234)
...
--- org.apache.commons.lang.text.StrBuilderTest::testLang294
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.apache.commons.lang.text.StrBuilder.deleteImpl(StrBuilder.java:1114) # 需匹配行
at org.apache.commons.lang.text.StrBuilder.deleteAll(StrBuilder.java:1188)
at org.apache.commons.lang.text.StrBuilderTest.testLang294(StrBuilderTest.java:1734)
...
--- org.apache.commons.lang.EntitiesTest::testNumberOverflow
junit.framework.ComparisonFailure: expected:<[�]> but was:<[慎]>
at junit.framework.Assert.assertEquals(Assert.java:100) # 需匹配行
at junit.framework.Assert.assertEquals(Assert.java:107)
at junit.framework.TestCase.assertEquals(TestCase.java:269)
at org.apache.commons.lang.EntitiesTest.doTestUnescapeEntity(EntitiesTest.java:101)
at org.apache.commons.lang.EntitiesTest.testNumberOverflow(EntitiesTest.java:201)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...
一个文件n段错误信息,匹配每段第一个 at 所在行,但排除特定字符,例如:Native Method
自己尝试用 grep 始终写不好,希望大佬给点建议,难道只能通过多次匹配实现吗
参考GPT和自己的思路:
可以使用 grep 命令结合正则表达式来实现这个匹配过程。具体步骤如下:
grep "at " [文件名] > temp.txt
grep -E '^((?!Native Method).)*$' temp.txt > temp_filtered.txt
sed -n '0,/at /!d;/Native Method/d;p;q' temp_filtered.txt
解释一下上面的 sed 命令:
最终的命令如下:
grep "at " [文件名] | grep -E '^((?!Native Method).)*$' | sed -n '0,/at /!d;/Native Method/d;p;q'
这样就可以实现一次匹配得到每段第一个匹配的行,并排除特定字符。
参考GPT和自己的思路:
你可以使用 grep 命令结合正则表达式来实现匹配第一个 at 所在行,但排除特定字符的功能。具体命令如下:
grep -Eo '^[^#]*at[^(]*([^N]*)'
解释一下这个命令:
这样就可以匹配到每段的第一个 at 所在行,并且排除了包含 Native Method 的行。