LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 11-03-2012, 06:18 AM   #1
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Rep: Reputation: Disabled
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
 
Old 11-03-2012, 06:54 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 11-03-2012, 07:08 AM   #3
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
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.

Last edited by hbchok; 11-03-2012 at 07:26 AM.
 
Old 11-03-2012, 09:16 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 11-03-2012, 09:28 AM   #5
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
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.
 
Old 11-04-2012, 06:50 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use grep to find two words near each other? walterbyrd Linux - Software 4 12-09-2011 11:01 PM
grep multiple words and linking with other file zonah12 Linux - Newbie 11 11-17-2011 10:29 PM
grep multiple words any order (AND, not OR) single line, from many files cedardoc Linux - Newbie 7 07-29-2010 10:23 AM
Grep an entire file but must contain multiple words wakeboarder3780 Linux - Newbie 10 02-19-2009 04:46 PM
GREP - reg exp to find words ending with .V and .TO vikram_cvk Linux - Server 5 12-22-2006 01:47 PM


All times are GMT -5. The time now is 05:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration