LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
  Search this Thread
Old 09-14-2016, 03:19 PM   #1
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Rep: Reputation: Disabled
search file for a word, if found run script1, if not found run script 2


i need to search a file and if it contains a certain word then execute script 1 and if NOT found execute script 2. What procedure would i use to search for a word in a file? examples of doing this sort of thing would also be helpful.
thank you - big tony
 
Old 09-14-2016, 03:41 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
What have you tried so far?
 
Old 09-14-2016, 03:58 PM   #3
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Original Poster
Rep: Reputation: Disabled
i have tried the below, which tells me that there are 2 words "PASSED" in the file. This is the answer i want.
i do not know how to run this so that if the output is 2 then run script1 and if the output is NOT 2 to run script2
your guidance would be appreciated. - thanks tony

working expression below:
cat edge_summary.txt | grep -c PASSED
2
 
Old 09-14-2016, 04:37 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,601

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Given
Code:
bash-4.3$ cat date.txt
Sun Sep  6 13:16:38 AEST 2015
then
Code:
bash-4.3$  grep -q Aug date.txt && echo "$? Found" || echo "$? Absent"
1 Absent
bash-4.3$  grep -q Sep date.txt && echo "$? Found" || echo "$? Absent"
0 Found

Last edited by allend; 09-14-2016 at 04:49 PM.
 
1 members found this post helpful.
Old 09-14-2016, 09:16 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
If the number of found entries is important, store this information in a variable and then perform a simple 'if' to test equal or not.
 
Old 09-14-2016, 09:29 PM   #6
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Original Poster
Rep: Reputation: Disabled
so how do i store the number of found entries in a variable
and how do i use if to test for equal or not equal?
 
Old 09-14-2016, 09:45 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,563
Blog Entries: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by bigtony View Post
so how do i store the number of found entries in a variable
and how do i use if to test for equal or not equal?
How? By reading, of course.

Variables
BASH Programming - Introduction HOW-TO
rtmistler's Bash Scripting for Dummies and Geniuses

Read / Study / Ask.
 
Old 09-15-2016, 12:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Any search on pretty much any search engine would answer all those questions .. at least try and put in some effort.
 
Old 09-15-2016, 08:07 AM   #9
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,601

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Quote:
the number of found entries
Is that the number of lines,
Code:
(( $(grep -c Aug date.txt) == 2 )) && ...
or, the number of occurrences?
Code:
(( $(grep -o Aug date.txt | wc -l) == 2 )) && ...
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script help -- Script2 calls Script1 and supplies input to script1. ckacosta Linux - General 1 11-02-2010 03:21 PM
Loop or check SQlite3 database continuously, when query found run script for first 3 Techno Guy Linux - Newbie 3 09-27-2009 07:46 PM
Run commands found in plain text file splunk Linux - Software 6 02-02-2008 11:15 AM
Can't seem to run a simple shell script error command not found each time SimonT Linux - Newbie 5 10-29-2006 06:31 AM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration