Grep for strings

Hi,

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36

How can I grep for the strings chrome and safari from a file, and if chrome print Chrome/40.0.2214.94 to a file and also count the number of times chrome is found?

Thanks

Here is EXACTLY what you asked for:

awk '/chrome/ {C++} /safari/ {} END {if (C) print "Chrome/40.0.2214.94", C > "file"}' file

what if the
40.0.2214.94 can change, so I would want a list of the different version numbers?

That is what was requested as long as "chrome" cannot appear on an input line more than once. If the RE could appear on a line multiple times, you'd need to change:

/chrome/ {C++}

to something more like:

{C += gsub(/chrome/, "&")}

And, although it is what was requested, cyberfrog should be warned that (as requested) this destroys the original contents of the input file.

Given the suggestions that you have received, what do you think you would need to do to count and list different version numbers? (Or, as in your original request, do you just want to count occurrences of "chrome" and print a different version number when you replace the contents of your input file with the number of occurrences of "chrome" (with or without a version number) that were in the file before you overwrite it?)