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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
09-12-2012, 12:51 PM
#1
Member
Registered: Aug 2011
Posts: 33
Rep:
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.
09-12-2012, 01:07 PM
#2
Member
Registered: Jun 2012
Location: Italy
Distribution: Slackware 13.37
Posts: 45
Rep:
I don't know. Can you do a example?
What must out the if condition?
09-12-2012, 01:27 PM
#3
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Sorry, I've just seen this and it confuses me -- what does this do?
09-12-2012, 01:29 PM
#4
Member
Registered: Aug 2011
Posts: 33
Original Poster
Rep:
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.
09-12-2012, 01:32 PM
#5
Member
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35
Rep:
grep -i "word" file ?
look into REGEX as well, that will help out.
09-12-2012, 01:58 PM
#6
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Quote:
Originally Posted by
brian00
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
would result in
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 .
09-12-2012, 02:01 PM
#7
Member
Registered: Aug 2011
Posts: 33
Original Poster
Rep:
grep would return:
oracle 12689 1 0 Sep05 ? 00:00:09 MYDB
how can I trim everything so I get only "MYDB"?
thx
09-12-2012, 02:01 PM
#8
Member
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35
Rep:
If you find out let me know, I need o dabble in scripting too.
09-12-2012, 02:15 PM
#9
Member
Registered: Aug 2011
Posts: 33
Original Poster
Rep:
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
09-12-2012, 02:17 PM
#10
Member
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35
Rep:
MYDB$ $ should look for " " at the end of a string. If I remember correctly.
09-12-2012, 02:21 PM
#11
Member
Registered: Aug 2011
Posts: 33
Original Poster
Rep:
sorry, I don't get it......
09-12-2012, 02:21 PM
#12
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Quote:
Originally Posted by
brian00
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
is
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 .
09-12-2012, 02:29 PM
#13
Member
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35
Rep:
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.
09-12-2012, 02:34 PM
#14
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Quote:
Originally Posted by
2armz
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?
09-12-2012, 03:12 PM
#15
Member
Registered: Sep 2012
Location: Garner NC
Distribution: Fedora 17
Posts: 35
Rep:
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 04:28 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News