Shell-Script

如何以不同格式過濾具有相同日期的行

  • October 16, 2017

我有一個這樣的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腳本可以嗎?

一種方法:

grep -E '/([0-9]{6})\.([0-9]{2}).* TAG\1\2' file

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

引用自:https://unix.stackexchange.com/questions/398393