例如:如果下面是我的日志文件的内容,我只想保留第一次出现的值“ 2018”和最后一次出现的“ 2018”之间的内容
sssssssssssssssssssss
ddddddddddddddddddddd
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018aaaaa
输出应为:
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
答案 0 :(得分:1)
如果文件不是太大,可以使用GNU grep和-z
标志来实现。来自grep.info
:
‘-z’
‘--null-data’
Treat input and output data as sequences of lines, each terminated
by a zero byte (the ASCII NUL character) instead of a newline.
Like the ‘-Z’ or ‘--null’ option, this option can be used with
commands like ‘sort -z’ to process arbitrary file names.
例如:
grep -zo '2018.*2018' infile
输出:
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
答案 1 :(得分:1)
检查此Perl解决方案
> cat 2018_match
sssssssssssssssssssss
ddddddddddddddddddddd
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018aaaaa
> perl -e ' BEGIN { $x=qx(cat 2018_match);$x=~s/(.+?)(2018.+2018)(.+)/$2/osmg; print "$x\n" ; exit } '
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
>