Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How can I grep a line for multiple instances of the same string?

Tags:

regex

grep

bash

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?

like image 862
BubbleMonster Avatar asked Sep 24 '15 12:09

BubbleMonster


2 Answers

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.

like image 144
Wiktor Stribiżew Avatar answered Sep 19 '22 18:09

Wiktor Stribiżew


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 <, <=, >, >=

like image 39
Kent Avatar answered Sep 20 '22 18:09

Kent