onur
onur

Reputation: 6365

How can I grep date value in complex pattern?

I use this grep for date:

grep -oP '(?<!\d)201\d{9}'

It's work for this:

xxx_yyy/zzz/www201504091545-xxx.tar.gz

But I can't grep date value when line does not have 201, like this:

/data/xxxx/yyyy/zzzz/T111504091545.ZET.gz
/data/xxxx/yyyy/zzzz/Z251504081345.YAT.gz
/data/xxxx/yyyy/zzzz/ZT51504071235.YAT.gz
/data/xxxx/yyyy/zzzz/ZA51403021131.YAT.gz

1504.... is date value(or 1403, 1404...). And after date value have .ZET.gz or .YAT.gz.

Before date, have 3 characters(number and/or not).

How can I do that? Thank you.

Outputs:

1504091545
1504081345
1504071235
1403021131

Upvotes: 0

Views: 50

Answers (1)

anubhava
anubhava

Reputation: 785098

You can use this grep:

grep -oP '\d{10}(?=\.)' file
1504091545
1504081345
1504071235
1403021131

Or little more restrictive as per your question:

grep -oP '\d{10}(?=\.(YAT|ZET))' file

Upvotes: 1

Related Questions