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-12-2012, 12:51 PM   #1
brian00
Member
 
Registered: Aug 2011
Posts: 33

Rep: Reputation: Disabled
search for key word from a file


I have writing a ksh and basically, I want to reach for a file (output.tst) and seach for the key word "MYDB"

Code:
if [[ -n `ls $log/output.tst | grep "MYDB" | grep "ASH"` ]] ; then
......

doesn't seem to work, what did I do wrong.

Thanks all.
 
Old 09-12-2012, 01:07 PM   #2
silendo
Member
 
Registered: Jun 2012
Location: Italy
Distribution: Slackware 13.37
Posts: 45

Rep: Reputation: Disabled
I don't know. Can you do a example?
What must out the if condition?
 
Old 09-12-2012, 01:27 PM   #3
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,676

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Sorry, I've just seen this and it confuses me -- what does this do?
Code:
ls $log/output.tst
 
Old 09-12-2012, 01:29 PM   #4
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
sorry if I confused you.

Basically, I want search for the file and look into the file to make sure those two key words are there.
 
Old 09-12-2012, 01:32 PM   #5
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
grep -i "word" file ?

look into REGEX as well, that will help out.
 
Old 09-12-2012, 01:58 PM   #6
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,676

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Quote:
Originally Posted by brian00 View Post
sorry if I confused you.

Basically, I want search for the file and look into the file to make sure those two key words are there.
I was just confused as to what that command usually returns. I'm new to scripting myself but I would have thought that
Code:
ls log/output.tst
would result in
Code:
output.tst
so I wondered what the dollar was doing? I would expect to see something like
Code:
cat log/output.tst | grep "MYDB"

Last edited by 273; 09-12-2012 at 02:04 PM.
 
Old 09-12-2012, 02:01 PM   #7
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
grep would return:

oracle 12689 1 0 Sep05 ? 00:00:09 MYDB

how can I trim everything so I get only "MYDB"?

thx
 
Old 09-12-2012, 02:01 PM   #8
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
If you find out let me know, I need o dabble in scripting too.
 
Old 09-12-2012, 02:15 PM   #9
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
how can I search for the string within a file?

Code:
if [[ -n `ls $log/output.tst | grep "MYDB"` ]] then 
do something.......
basically, I want to look in the file output.tst with the string name "MYDB", if it found "MYDB", then do something
 
Old 09-12-2012, 02:17 PM   #10
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
MYDB$ $ should look for " " at the end of a string. If I remember correctly.
 
Old 09-12-2012, 02:21 PM   #11
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
sorry, I don't get it......
 
Old 09-12-2012, 02:21 PM   #12
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,676

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Quote:
Originally Posted by brian00 View Post
grep would return:

oracle 12689 1 0 Sep05 ? 00:00:09 MYDB

how can I trim everything so I get only "MYDB"?

thx
But the result of
Code:
ls log/output.tst | grep "MYDB"
will be nothing since the output of
Code:
ls log/output.tst
is
Code:
output.tst
Does the '$' on the front somehow pass the contents of the file instead of its name?

Last edited by 273; 09-12-2012 at 02:22 PM.
 
Old 09-12-2012, 02:29 PM   #13
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
The $ should go at the end so it searches for MYDB. So grep MYDB$ would search for strings ending in MYDB. You can "man grep" for more rules for it.
 
Old 09-12-2012, 02:34 PM   #14
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,676

Rep: Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912Reputation: 1912
Quote:
Originally Posted by 2armz View Post
The $ should go at the end so it searches for MYDB. So grep MYDB$ would search for strings ending in MYDB. You can "man grep" for more rules for it.
But how does piping the result of ls to grep that way result in grep searching the file?
 
Old 09-12-2012, 03:12 PM   #15
2armz
Member
 
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35

Rep: Reputation: Disabled
hmm I might not be on the same page as you, i kinda understand what you are asking, but I'm new to this as well, so if i've thrown you off track sorry
 
  


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
How to build a script that will search for a specific word in a log file kimbo8 Programming 1 03-27-2012 05:17 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
search file contains word prospekrisal Linux - Newbie 5 07-20-2006 09:24 PM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM


All times are GMT -5. The time now is 10:15 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