Grep for strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for strings
# 1  
Old 03-06-2015
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
# 2  
Old 03-06-2015
Here is EXACTLY what you asked for:
Code:
awk '/chrome/ {C++} /safari/ {} END {if (C) print "Chrome/40.0.2214.94", C > "file"}' file

# 3  
Old 03-06-2015
what if the
40.0.2214.94 can change, so I would want a list of the different version numbers?
# 4  
Old 03-06-2015
Quote:
Originally Posted by RudiC
Here is EXACTLY what you asked for:
Code:
awk '/chrome/ {C++} /safari/ {} END {if (C) print "Chrome/40.0.2214.94", C > "file"}' file

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:
Code:
/chrome/ {C++}

to something more like:
Code:
{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.
# 5  
Old 03-06-2015
Quote:
Originally Posted by cyberfrog
what if the
40.0.2214.94 can change, so I would want a list of the different version numbers?
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?)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep strings for different cases

Hi All, Good morning I have a below code which is working & getting expected output. the problem in this code is it is executing 3 if conditions, my requirement is suppose if first condition is success then it should print echo statement & exit from if condition else if the 1st if condition... (4 Replies)
Discussion started by: sam@sam
4 Replies

2. Shell Programming and Scripting

Grep process with two strings

Hi, I am trying to get the PID of a process that is defunct. I have two quesitons on this. 1) how do I pass in two strings? This is what I am using now: ps -ef |egrep -i "programname|<defunct>" But this returns multiple results with the word defunct in them. 2) How do I make it so that... (1 Reply)
Discussion started by: ChickenPox
1 Replies

3. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

4. Red Hat

Grep between two strings in shell

<cisco:subname> <cisco:sptp>Cisco PortA Series</cisco:sptp> <cisco:aliasNameList xsi:nil="true"/> <cisco: owner xsi:nil="true"/> <cisco:subportname> <cisco:cpt>Cisco SubPort B Series</cisco:cpt> ... (3 Replies)
Discussion started by: itsspy
3 Replies

5. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

6. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

7. Shell Programming and Scripting

grep two strings in a file..

Hello All, I have a big file about 1000 lines. Now i am trying to grep a particular string and printing the lines from the string. say for example in 500th line i have the date as "Mon Wed 14 20:15:24 2010". now i in my case i need to grep the combination of the strings "Mon Wed 14" and the... (7 Replies)
Discussion started by: intiraju
7 Replies

8. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

9. Shell Programming and Scripting

want to grep only strings in a file?

Hai, Just want to print only alphanumeric in a file ex:- fdsdsklf#@^%$#hf output:- fdsdsklfhf plz, help me:o (5 Replies)
Discussion started by: balan_mca
5 Replies

10. UNIX for Dummies Questions & Answers

Using grep to find strings of certain lengths?

I am trying to use grep to find strings of certain lengths that all start with the same letter. Is this possible?:confused: (4 Replies)
Discussion started by: crabtruck
4 Replies
Login or Register to Ask a Question