LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep a file and find multiple words and take action accordingly. (http://www.linuxquestions.org/questions/linux-newbie-8/grep-a-file-and-find-multiple-words-and-take-action-accordingly-4175435441/)

hbchok 11-03-2012 06:18 AM

grep a file and find multiple words and take action accordingly.
 
Hi,

I am very new to Linux and don't know programming.
But by reading some things I can understand so I am able to question.

I am doing something like below,

find few words in a file and take action accordingly

find in log.txt
if "not found" 1 > notify.txt
if "reset by peer" 2 > notify.txt
if "Permission denied" 3 > notify.txt
if "Fetching" 0 > notify.txt
exit

please help me to write the script.

Thanks

pixellany 11-03-2012 06:54 AM

Please confirm what you want to do---the "pseudocode" that you provided appears to say this:
in the file log.txt:
if the string "not found" is found, then write the numeral 1 to a file named notify.txt
OR...

did you mean that standard output should be redirected to notify.txt?

Also, did you intend that the file be written from the beginning, or should the data be appended to what is already there?

Finally, why GREP? I think this situation is better handled by some other utilities

hbchok 11-03-2012 07:08 AM

Quote:

Originally Posted by pixellany (Post 4821330)
Please confirm what you want to do---the "pseudocode" that you provided appears to say this:
in the file log.txt:
if the string "not found" is found, then write the numeral 1 to a file named notify.txt
OR...

did you mean that standard output should be redirected to notify.txt?

Also, did you intend that the file be written from the beginning, or should the data be appended to what is already there?

Finally, why GREP? I think this situation is better handled by some other utilities

I want to write in notify.txt 0 or 1 or 2 or 3.
Now script will decide what to write.
script will decide by reading log.txt file.

if [it found] "not found" [text in the log.txt then it will write]1 > notify.txt
if [it found] "reset by peer" [text in the log.txt then it will write] 2 > notify.txt
if [it found] "Permission denied" [text in the log.txt then it will write] 3 > notify.txt
if [it found] "Fetching" [text in the log.txt then it will write] 0 > notify.txt

By this way I could create notification using notify.txt through edf.

About grep I will say that I know only this one thing.. If you can advice me something else then thanks a lot.
But I need to use that in .sh script so if you will advice me some thing else then you need to tell 2 things.

1. how to use that utility for this task.
2. what text could be in script.

Hop I could explain you properly on this time.

Thanks.

pixellany 11-03-2012 09:16 AM

My hunch is that you need the "case" construct in BASH:
http://www.tldp.org/LDP/Bash-Beginne...ect_07_03.html

The idea is that each condition is assigned a code block to define what happens in that case.

Note: Be sure you are clear about the difference between writing to a file and appending to the file.

Before proceding, I recommend reading all of the Bash-Beginners-Guide linked above.

hbchok 11-03-2012 09:28 AM

Quote:

Originally Posted by pixellany (Post 4821377)
My hunch is that you need the "case" construct in BASH:
http://www.tldp.org/LDP/Bash-Beginne...ect_07_03.html

The idea is that each condition is assigned a code block to define what happens in that case.

Note: Be sure you are clear about the difference between writing to a file and appending to the file.

Before proceding, I recommend reading all of the Bash-Beginners-Guide linked above.

Right, I want to overwrite the "notify.txt" file, so always I can get only one digit and most updated status.
Requesting you to please ask me if my question is not clear.

Thanks for the link.

chrism01 11-04-2012 06:50 PM

Good link; see also http://rute.2038bug.com/index.html.gz & http://www.tldp.org/LDP/abs/html/


All times are GMT -5. The time now is 11:07 PM.