Friday, July 22, 2022

[SOLVED] grep -E {,1} showing results that have more than 1 occurrence

Issue

I've been trying to figure out how to grep a line with only N occurrences of a character.

[root@example DIR]# grep -E "6{,1}" test.txt
6543
6625
6668
6868
6666
1161

What I want is for grep to print out the following:

[root@example DIR]# grep -E "6{,1}" test.txt
6543
1161

What am I missing?


Solution

The "what am I missing" part is: Regular expressions will ignore trailing context once a match is found unless you tell them not to. So 666 matches 6{,1} because the first 6 matches and you have not said you don't want to allow additional 6 instances after that occurrence.

You could use a more-complex regex like ^([^6]*6){,1}[^6]*$ but I would actually tackle this with Awk:

awk -F 6 'NF==2' file

will find all lines in file which have exactly one occurrence of 6.

We cleverly use 6 as the field delimiter and examine the number of fields when Awk has read a line and split it into fields. The variable NF conveniently tells us the number of resulting fields.

Awk programs have the form condition { action } where both parts are optional. With no condition, the { action } is taken on every input line. With no { action }, the default action is to print lines which meet the condition. Here, the condition is NF==2.



Answered By - tripleee
Answer Checked By - Gilberto Lyons (WPSolving Admin)