Display a few lines before selected word
The following command is correctly returning all the lines with warnings more than 0 from all files.
grep -i warning * | grep -v 'Warnings: 0' | more
I want to see t开发者_JAVA百科he 4 lines above the warnings line where warning is more than 0. The -B4 switch does not work for obvious reasons.
If I understood your question correctly, here is a solution:
grep -v "Warnings: 0" * | grep -B4 -i warning
How about using a little regex instead:
grep -e "Warnings: [1-9][0-9]*" -B4 * | more
The grep should look for 1 or more warnings and print the previous 4 lines.
your are unclear if the warnings message should be include in the output or not. In this version it is.
Try:
awk -F"[: ]" '($1 /Warning/ && $2> 0){a[NR]=$0;for (i=4;i>=0;i--) \
print a[NR-i]}{a[NR]=$0}' file
HTH Chris
精彩评论