fattel
fattel

Reputation: 153

How to match pattern with any number higher than 1

I have text file with:

NH:i:1
NH:i:2
NH:i:3
NH:i:4
NH:i:9
NH:i:10
NH:i:11
NH:i:12
NH:i:1002

I would like to in unix one liner exclude lines if the number is different than 1 at the end. Potentially it can be number with any number of digits. I do not want to extract specifically NH:i:1 as I also have lines without this pattern that I want to keep. thank you i advance

Upvotes: 0

Views: 32

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

You can use

grep -E '(^|[^0-9])([2-9]|[1-9][0-9]+)$' file

See the online demo:

s='NH:i:1
NH:i:2
NH:i:3
NH:i:4
NH:i:9
NH:i:10
NH:i:11
NH:i:12
NH:i:1002'
grep -E '(^|[^0-9])([2-9]|[1-9][0-9]+)$' <<< "$s"

Output:

NH:i:2
NH:i:3
NH:i:4
NH:i:9
NH:i:10
NH:i:11
NH:i:12
NH:i:1002

Upvotes: 1

Related Questions