tags:

views:

1011

answers:

5

I need to grep for lines with bunch of names, say "[email protected]", "[email protected]" from a file.txt file.txt has junk which is [email protected] [email protected]. I need to filter these out

Once I get these lines I need to grep for gmail and yahoo and get their counts

List l = new ArrayList{[email protected], [email protected]}
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 doesn't work. How can I substitute the $l.get(1) value dynamically?

+1  A: 

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

def client = '[email protected]'
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's documentation may also assist you with this.

Rob Hruska
A: 

file.txt has lot of junk and a pattern something like "Into the domain [email protected] exit on 12/01/2008 etc.."

hence I do

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

that way I can chain the grep as I want and eventually land at the count I need.

I am not sure if I can chain the greps if I use

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

thanks for your input.

adantu
Once you have the grep results you could throw them in a loop within the groovy and just perform string operations on them, too.Also note that in order for groovy to realize that you want $client to be replaced it must be inside double-quotes instead of single quotes.
Rob Hruska
A: 

You need {}'s around the variable expression. That is:

 "${l.get(0)}"

See Groovy String documentation for more information.

Full example:

List l = new ArrayList{[email protected], [email protected]}
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 ]
flicken
A: 

the problem is that ${l.get(0)} has to be inside the " ", i.e. def gmail = ['sh','-c','grep "clientLogin=${l.get(0)}" file.txt' | grep gmail | wc -l ] so that it will look like def gmail = ['sh','-c','grep "[email protected]" file.txt' | grep gmail | wc -l ] but "clientLogin=${l.get(0)}" doesn't produce the result. I am not sure where I am going wrong.

Thanks for your suggestion but it doesn't produce the result, at least when I tried it.

adantu
+1  A: 

You're already using groovy, does using a regular expression that gives you your answer work?

def file = new File("file.txt")    
file.delete() // clear out old version for multiple runs
file <<  """
foobar [email protected] baz quux   # should match [email protected]
foobar [email protected] baz quux
foobar [email protected] bal zoom
foobar [email protected] baz quux   # should match [email protected]
foobar [email protected] bal zoom   # should match [email protected]
foobar [email protected] bal zoom
"""

def emailList = ["[email protected]", "[email protected]"]
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["[email protected]"] == 2
assert resultMap["[email protected]"] == 1

That feels cleaner to me than trying to shell out to a process and working with that, plus it only will pick out the exact lines with "clientLogin=(email)" that you're looking for.

Ted Naleid