How to use a variable in grep with groovy?

I need grep for strings with a bunch of names, for example clientLogin=a@yahoo.com , clientLogin=b@gmail.com from a .txt file.

file.txt has garbage that is email=a@yahoo.com email=b@gmail.com . I need to filter them

As soon as I get these lines, I need grep for gmail and yahoo and get their number

 List l = new ArrayList{ a@yahoo.com , b@gmail.com } def gmail = ['sh','-c','grep "clientLogin="$l.get(0) file.txt' | grep gmail | wc -l ] def yahoo = ['sh','-c','grep "clientLogin="$l.get(1) file.txt' | grep yahoo| wc -l ] 

This does not work. How can I substitute the value of $ l.get (1) dynamically?


the problem is that $ {l.get (0)} must be inside "", that is :.

 def gmail = ['sh','-c','grep "clientLogin=${l.get(0)}" file.txt' | grep gmail | wc -l ] 

so that it looks like this:

 def gmail = ['sh','-c','grep " clientLogin=a@yahoo.com " file.txt' | grep gmail | wc -l ] 

but clientLogin=${l.get(0)} gives no result. I am not sure where I am going wrong.

Thank you for your suggestion, but it does not give a result, at least when I tried it.


file.txt has a lot of garbage and a template something like:

 Into the domain clientLogin=a@yahoo.com exit on 12/01/2008 etc.. 

therefore i do

 def ex = ['sh','-c','grep "domain clientLogin=$client" file.txt'| grep "something more" | wc -l] 

that way I can bind grep the way I want and end up landing on the account I need.

I'm not sure if I can link greps if I use

 def ex = ['grep', "$client", 'file.txt'] 

Thanks for your input.

+3
source share
3 answers

Are you already using groovy using a regex that gives you your answer?

 def file = new File("file.txt") file.delete() // clear out old version for multiple runs file << """ foobar clientLogin=a@yahoo.com baz quux # should match a@yahoo.com foobar email=a@yahoo.com baz quux foobar email=b@gmail.com bal zoom foobar clientLogin=a@yahoo.com baz quux # should match a@yahoo.com foobar clientLogin=b@gmail.com bal zoom # should match b@gmail.com foobar email=b@gmail.com bal zoom """ def emailList = [" a@yahoo.com ", " b@gmail.com "] def emailListGroup = emailList.join('|') def pattern = /(?m)^.*clientLogin=($emailListGroup).*$/ def resultMap = [:] (file.text =~ pattern).each { fullLine, email -> resultMap[email] = resultMap[email] ? resultMap[email] + 1 : 1 } assert resultMap[" a@yahoo.com "] == 2 assert resultMap[" b@gmail.com "] == 1 

It seems to me that it is cleaner for me than trying to figure out the process and work with it, plus it will only select the exact lines with "clientLogin = (email)" that you are looking for.

+4
source

I'm not sure if you need "sh" and "-c". I was able to get this to work:

 def client = ' foo@bar.com ' def ex = ['grep', "$client", 'file.txt'] def proc = ex.execute() proc.waitFor() println "return: ${proc.exitValue()}" println "stderr: ${proc.err.text}" println "stdout: ${proc.in.text}" 

Groovy documentation can also help you with this.

+2
source

You need {} around the variable expression. I.e:

  "${l.get(0)}" 

See Groovy String for more details.

Full example:

 List l = new ArrayList{ a@yahoo.com , b@gmail.com } def gmail = ['sh','-c','grep "clientLogin="${l.get(0)} file.txt' | grep gmail | wc -l ] def yahoo = ['sh','-c','grep "clientLogin="${l.get(1)} file.txt' | grep yahoo| wc -l ] 
0
source

All Articles