Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep -E {,1} showing results that have more than 1 occurance

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?

like image 837
Keith C. Avatar asked Mar 08 '23 00:03

Keith C.


1 Answers

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.

like image 195
James Brown Avatar answered Mar 10 '23 09:03

James Brown