grep a file and find multiple words and take action accordingly.
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
please help me to write the script.
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
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
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.
My hunch is that you need the "case" construct in BASH:
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.
Requesting you to please ask me if my question is not clear.
Thanks for the link.
|All times are GMT -5. The time now is 07:34 PM.|