grep -E {,1} showing results that have more than 1 occurrence
With awk I'd:
$ awk '/6/&&!/6.*6/' file
6543
1161
It translates to grep
like:
$ grep 6 file | grep -v 6.*6
6543
1161
Edit:
@Sundeep's clever idea to use 6 as a field separator and to count the fields (see comments):
$ awk -F6 'NF==2' file
6543
1161
^ his comment below.
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
.