LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search for key word from a file (https://www.linuxquestions.org/questions/linux-newbie-8/search-for-key-word-from-a-file-4175426917/)

brian00 09-12-2012 12:51 PM

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.

silendo 09-12-2012 01:07 PM

I don't know. Can you do a example?
What must out the if condition?

273 09-12-2012 01:27 PM

Sorry, I've just seen this and it confuses me -- what does this do?
Code:

ls $log/output.tst

brian00 09-12-2012 01:29 PM

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.

2armz 09-12-2012 01:32 PM

grep -i "word" file ?

look into REGEX as well, that will help out.

273 09-12-2012 01:58 PM

Quote:

Originally Posted by brian00 (Post 4778734)
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"

brian00 09-12-2012 02:01 PM

grep would return:

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

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

thx

2armz 09-12-2012 02:01 PM

If you find out let me know, I need o dabble in scripting too.

brian00 09-12-2012 02:15 PM

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

2armz 09-12-2012 02:17 PM

MYDB$ $ should look for " " at the end of a string. If I remember correctly.

brian00 09-12-2012 02:21 PM

sorry, I don't get it......

273 09-12-2012 02:21 PM

Quote:

Originally Posted by brian00 (Post 4778762)
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?

2armz 09-12-2012 02:29 PM

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.

273 09-12-2012 02:34 PM

Quote:

Originally Posted by 2armz (Post 4778790)
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?

2armz 09-12-2012 03:12 PM

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 ;)


All times are GMT -5. The time now is 09:19 PM.