linux 处理匹配文本的前后行 | 您所在的位置:网站首页 › awk删除匹配行 › linux 处理匹配文本的前后行 |
对于小文件,可以翻转后正序删除 tac file | sed '/tom/I,+2 d' | tac #即删除不区分大小写匹配到有tom的行,同时删除向上两行 grep grep -A 2 匹配后继续向下打印2行 -B 2 匹配后继续向上打印2行 -C 2 向上向下各打印2行 awk下面awk脚本可删除指定具体匹配行的前n行 #!/bin/sh # grep-ac: a grep-like awk script # Arguments: pattern = awk regexp to search for # before = number of lines to print before a match # after = number of lines to print after a match { "exec" "awk" "-f" "$0" "$@"; } # The array h contains the history of lines that haven't been printed # but are eligible for being "before" lines. # The variable until contains the number of the last "after" line to print. match($0, pattern) { # the current line matches for (i in h) { print h[i]; # print each remaining before line delete h[i]; # delete each line as it's printed } until=NR+after; # record the last after line to print } { if (NR |
CopyRight 2018-2019 实验室设备网 版权所有 |