LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 07-19-2012, 02:52 PM   #1
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 16

Rep: Reputation: Disabled
limit result of a grep command using the w switch


Hello, I want this result to only show me app1 but I'm also getting app11. How can I limit it?

grep -w app1 prod*cmds|grep state
prod01.cmds:state com.rots.app1.vTRZORS app11
prod05.cmds:state com.dests.zeb.vTRZORS app1

I guess I'm getting the app11 output b/c of the ".app1."(bolded) above. Can anybody think of a way for me to only get "app1" as the result?

I know there are several ways to get what I'm looking for if this was case specific, ie "grep ' app1$' but don't want that, I am using this in a script so don't want it to be a case specific. And using field delimiters is also not an option bc the script actually is trying to determine which host the app1 process runs(prod05).

Thx.
 
Old 07-19-2012, 03:07 PM   #2
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by amateurscripter View Post
Hello, I want this result to only show me app1 but I'm also getting app11. How can I limit it?

grep -w app1 prod*cmds|grep state
prod01.cmds:state com.rots.app1.vTRZORS app11
prod05.cmds:state com.dests.zeb.vTRZORS app1

I guess I'm getting the app11 output b/c of the ".app1."(bolded) above. Can anybody think of a way for me to only get "app1" as the result?

I know there are several ways to get what I'm looking for if this was case specific, ie "grep ' app1$' but don't want that, I am using this in a script so don't want it to be a case specific. And using field delimiters is also not an option bc the script actually is trying to determine which host the app1 process runs(prod05).

Thx.
Hi scripter,

what's wrong with using "grep -w app1$"? The $ just stands for end-of-line, and it shouldn't make anything case-specific that isn't already case-specific in your original command. You can use -i ("grep -wi app1$") to really ignore cases either way.

Regards
dmdeb
 
Old 07-19-2012, 03:21 PM   #3
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
I guess so, thx.
 
Old 07-19-2012, 04:20 PM   #4
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
-nevermind-
You can use a regular expression ".*app1[^1]*"
.* = any character
app1 = app1
[^1] = not 1 (after app1)
The options for grep -ow. o is print only the matching part of the regex (".*app1[^1]*") and w is whole words.
Code:
bash-4.2$ echo prod01.cmds:state com.rots.app1.vTRZORS app11 |grep -ow ".*app1[^1]*"
prod01.cmds:state com.rots.app1.vTRZORS

Last edited by whizje; 07-19-2012 at 04:35 PM.
 
Old 07-19-2012, 04:43 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
You want line 2 and not line 1
Put a space before app1 and .* is anything before it.
Code:
bash-4.2$  echo prod01.cmds:state com.rots.app1.vTRZORS app11|grep -w "prod.*cmds.*[ ]app1"
bash-4.2$  echo prod01.cmds:state com.rots.app1.vTRZORS app1|grep -w "prod.*cmds.*[ ]app1"
prod01.cmds:state com.rots.app1.vTRZORS app1

Last edited by whizje; 07-19-2012 at 04:48 PM.
 
Old 07-20-2012, 01:04 PM   #6
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Sorry guys, these don't work b/c there's also an invisible character(^M) at the end of the line so using reg expersions is not an option. I can remove them for now but what happens later when someone else udates the file with (copy and paste mostly) with another invisible character for a different process name. I want the script to be general general solution not a specific case solution. how do i get grep to not return a result for the ".app1." match. That's where the issue is
 
Old 07-20-2012, 01:24 PM   #7
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by amateurscripter View Post
Sorry guys, these don't work b/c there's also an invisible character(^M) at the end of the line so using reg expersions is not an option. I can remove them for now but what happens later when someone else udates the file with (copy and paste mostly) with another invisible character for a different process name. I want the script to be general general solution not a specific case solution. how do i get grep to not return a result for the ".app1." match. That's where the issue is
Hey scripter,

I see where you're heading, but what exactly are the requirements? What invisible characters do you need to exclude, precisely?

If you want to allow any set of white space between "app1" and the end of the line, you can use this:

egrep -wi "app1[[:space:]]*$"

... which matches all lines ending with app1 followed by an arbitrary set of white-space characters.

Regards
dmdeb
 
Old 07-20-2012, 02:02 PM   #8
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
A regex is for general use.
What's wrong with this
Code:
grep -w "prod.*cmds.*[ ]app1"

Last edited by whizje; 07-20-2012 at 02:03 PM.
 
Old 07-20-2012, 03:32 PM   #9
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
This does the trick for me, thx all. Appreciate it.

---------- Post added 07-20-12 at 03:32 PM ----------

Oopps, forgot to paste what did the trick
grep -w "prod.*cmds.*[ ]app1"
 
  


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
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 03:39 PM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 01:09 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 06:28 AM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 01:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 08:47 AM.

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