1

An offshoot from this question:

While searching for the string "banana" from the following file, we would like 1,2,3 and 7 instances of lines 1,2,3 and 4 respectively. The number of grep outputs should equal the number of match instances while still returning the entire line.

There is one banana here
There are two banana banana here
There are three banana banana banana here
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
In fact we need not have any too!

Note: If we remove the restriction of whole lines in the output, we have:

grep -no "banana" tempfile 

which returns

1:banana
2:banana
2:banana
3:banana
3:banana
3:banana
4:banana
4:banana
4:banana
4:banana
4:banana
4:banana
4:banana

Any ideas?

EDIT: This is the intended output

1 There is one banana here
2 There are two banana banana here
2 There are two banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
5
  • 1
    Does it have to be grep? I can think of a way to do it in perl... Dec 20, 2016 at 20:13
  • 1
    Can you clarify what you mean by "repeat the whole line match" and maybe provide an example of what you expect output to be ? You want There are two banana banana here to be printed 2 times because there's 2 matches ? Dec 20, 2016 at 20:23
  • Is this what you wanted ? paste.ubuntu.com/23660761 Dec 20, 2016 at 20:25
  • @Serg Yes, that's exactly what I wanted.. Sorry for not being clear. I am editing my question now Dec 21, 2016 at 19:20
  • @ManojKumar In that case, please take time to mark one of the answers as accepted that provides the best solution Dec 21, 2016 at 19:28

3 Answers 3

3

Using the perl string repetition operator, obtaining the number of repeats by evaluating the result of the match in a scalar context:

$ perl -pe '$_ x= (() = /banana/g)' file
There is one banana here
There are two banana banana here
There are two banana banana here
There are three banana banana banana here
There are three banana banana banana here
There are three banana banana banana here
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
1
  • 1
    Thanks! But I am accepting the answer by @Serg because it is easier to understand. Dec 21, 2016 at 19:25
2

grep doesn't have counter for matches, only -c counter for lines that have the match, but we can use awk to do that. As far as I understand , you want to print the line that matches x number of times based on the amount of matches. Well, here it is:

$ awk '{for(i=1;i<=NF;i++) if($i=="banana") counter++;for(j=1;j<=counter;j++) print NR,$0;counter=0 }' input.txt         
1 There is one banana here
2 There are two banana banana here
2 There are two banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have

Basic idea here is that we loop over each word in a line, and count matches. If there's a match we increment counter and then use that counter to print the same line in a loop. Finally counter is reset and process repeats

2
  • This does not repeat the lines, as asked by OP. Dec 20, 2016 at 20:18
  • @AdrienBeau I've edited my answer, please see the changes, but so far OP hasn't been clear on what exactly they want as output, so I'll trust your comment that OP wants lines repeated. Dec 20, 2016 at 20:32
0

Its not very pretty, but you could use something like

awk '{print NR, gsub(/banana/, "")' <tempfile

It works by using the awk gsub command to substitute the match pattern (here, banana) and it returns the number of times that it performs the substitution. If then outputs the the input line number and the number of matches

Or to repeat the input line multiple times

awk '{A=$0
b=gsub(/banana/, "")
for (i=1; i<=b; i++) print A
}' <tempfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .