LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   limit result of a grep command using the w switch (https://www.linuxquestions.org/questions/linux-newbie-8/limit-result-of-a-grep-command-using-the-w-switch-4175417653/)

amateurscripter 07-19-2012 01:52 PM

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.

dmdeb 07-19-2012 02:07 PM

Quote:

Originally Posted by amateurscripter (Post 4732955)
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

amateurscripter 07-19-2012 02:21 PM

I guess so, thx.

whizje 07-19-2012 03:20 PM

-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


whizje 07-19-2012 03:43 PM

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


amateurscripter 07-20-2012 12:04 PM

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

dmdeb 07-20-2012 12:24 PM

Quote:

Originally Posted by amateurscripter (Post 4733869)
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

whizje 07-20-2012 01:02 PM

A regex is for general use.
What's wrong with this
Code:

grep -w "prod.*cmds.*[ ]app1"

amateurscripter 07-20-2012 02:32 PM

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"


All times are GMT -5. The time now is 02:20 AM.