我有一个这样的txt文件:
./201709.15.txt:88:word word TAG201709152000 word word
./201709.19.txt:3:word TAG201709152000 word word
./201710.10.txt:5:word word TAG201709152000 word word word
我只需要过滤以下行:
./201709.15.txt:88:word word TAG201709152000 word word
(即开头具有相同的日期:./YYYMM.dd.txt
并在 TAG: 之后TAGYYYYMMddhhmm
)
用shell脚本可以吗?
答案1
一种方法是:
grep -E '/([0-9]{6})\.([0-9]{2}).* TAG\1\2' file
答案2
awk解决方案:
awk -F'.' 'match($4,/TAG[0-9]{8}/) && substr($4,RSTART+3,RLENGTH-3) == substr($2$3,2)' file
输出:
./201709.15.txt:88:word word TAG201709152000 word word