I've got several lines that look like this:
aaaaaaaaxzaaaaaaaaaaaaaa
bbbbbbbbbbbbxzbbbbbbxzbb
ccxzcccccccccccccccccxzc
dddddddxzddddddddddddddd
Inside two of those lines, there are two instances of xz
characters. I want grep to look for xz
twice in the same line and output the lines that it matches on. If xz
appears once, I don't want to know.
Running the following:
cat lines | grep "xz"
Tells me every line with xz on, but I only want to see lines with xz appearing twice.
How can I make the pattern search repeat in the same line?
You can use
cat lines | grep 'xz.*xz'
Or just
grep 'xz.*xz' lines
The .*
will match optional characters (any but a newline) between 2 xz
.
In case you need to use look-arounds, you will need -P
switch to enable Perl-like regexps.
awk is one way to go:
awk -F'xz' 'NF==3' file
or
awk 'gsub(/xz/,"")==2' file
another benefit awk brings you is, it is easier to check a pattern matched less then n times
, exact n times
or greater than n times
. you just change the ==
into <, <=, >, >=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With