LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep string with space (2 word string) (http://www.linuxquestions.org/questions/linux-newbie-8/grep-string-with-space-2-word-string-749506/)

casperdaghost 08-22-2009 07:18 AM

grep string with space (2 word string)
 
i need to isolate a two word string seperated by one space. both words occur regularly in this file. all i need to do is isolate the lines that contain 'modify sent'. that is modify (space) sent. not just modify , and not just sent and not both words occuring in different places on one line, but just the two words together, - just 'modify sent'

I googled this and do not see much for a two word string seperated by a space bar - grep -w with quotes does not work. It seems easy but i can't figure it out. i searched for orderModified because it sometimes occurs with the string, but not always. i also made some vain attempts to modify the file (partial.eventlog vs evntlog.86453.partial)

two words seperated by a space bar, that is all i need to search for -


casper@kop11> egrep "modify sent" partial.eventlog | wc -l
0
casper@kop11> grep orderModified partial.eventlog | wc -l
0
casper@kop11> egrep orderModified evntlog.86453.partial | wc -l
685
casper@kop11> egrep "modify sent" evntlog.86453.partial | wc -l
685
casper@kop11> egrep "modify|sent" evntlog.86453.partial | wc -l
12623
casper@kop11> egrep "^modify * space" evntlog.86453.partial | wc -l
0
casper@kop11> grep "^modify sent$" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> egrep "^modify sent$\" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> egrep "/^modify sent$/" evntlog.86453.partial | wc -l
Illegal variable name.

casper@kop11> egrep -w "/^modify sent$/" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> grep '\modify |*sent\' evntlog.86453.partial | wc -l
Illegal variable name
casper@kop11>

markush 08-22-2009 07:34 AM

Hello casperdaghost,

try
Code:

egrep "modify sent" partial.eventlog
this should work.

Ok, you wrote this as if it works not. But for me it works.

Markus

SeSoX 08-22-2009 07:42 AM

The following two ways of doing it should work:

egrep "modify sent" partial.eventlog | wc -l

and

grep "modify sent" partial.eventlog | wc -l

If it does not work for you, there has to be some other problem, you can try to do it in a smaller text file that you have created to see if it's a problem with the file or with the command.

The only problems I can think of right now are that the file has some "weird" characters or that you have an alias for grep/egrep which is making it behave in a different way.

Hope that helps.
Regards.

andywebsdale 08-22-2009 08:00 AM

You could try single quotes 'modify sent' - I'm not sure but may be handled differently to "
I've just noticed you've single quoted the last try, but escaped the second quote causing an error maybe

catkin 08-22-2009 09:29 AM

Quote:

Originally Posted by andywebsdale (Post 3653432)
You could try single quotes 'modify sent' - I'm not sure but may be handled differently to "
I've just noticed you've single quoted the last try, but escaped the second quote causing an error maybe

Bash does handle single quoted strings differently from double quoted strings.

Everything in single quotes is untouched by the shell. Thus when the shell is given '\modify |*sent\' on the grep command it gives \modify |*sent\ to grep (as a single word, with the spaces included). Dead simple, bash just copies each character after a single quote until it gets to another single quote.

Strings in double quotes are more complex. Here's from the GNU bash Reference "Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed."

More complex, huh?!

If you don't quote strings then bash will look for a raft of things -- redirection, subshells, backgrounding, ...

So -- unless you need the power of double quoting, always use single quotes. KISS!

catkin 08-22-2009 09:38 AM

Hello casperdaghost :)

Is the screen session you quote exactly what appeared on your screen?
Quote:

Originally Posted by casperdaghost (Post 3653405)

Code:

casper@kop11> grep "^modify sent$" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> egrep "^modify sent$\" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> egrep "/^modify sent$/" evntlog.86453.partial | wc -l
Illegal variable name.

casper@kop11> egrep -w "/^modify sent$/" evntlog.86453.partial | wc -l
Illegal variable name.
casper@kop11> grep '\modify |*sent\' evntlog.86453.partial | wc -l
Illegal variable name
casper@kop11>


I get different results
Code:

c@CW8:~/d/tmp$ grep "^modify sent$" trash
c@CW8:~/d/tmp$ egrep "^modify sent$\" trash
>
c@CW8:~/d/tmp$ # Comment: I pressed Ctrl+C to terminate the unfinished double quoted string
c@CW8:~/d/tmp$ egrep "/^modify sent$/" trash
c@CW8:~/d/tmp$ egrep -w "/^modify sent$/" trash
c@CW8:~/d/tmp$ grep '\modify |*sent\' trash
grep: Trailing backslash

If it is exact then which shell are you using and do you have aliases for grep and egrep
Code:

c@CW8:~/d/tmp$ echo $SHELL
/bin/bash
c@CW8:~/d/tmp$ type grep
grep is hashed (/bin/grep)
c@CW8:~/d/tmp$ type egrep
egrep is hashed (/bin/egrep)


chrism01 08-23-2009 09:29 PM

Code:

grep 'modify sent' partial.eventlog | wc -l
should work.
I agree with catkin, use single quotes unless you truly want interpolation etc.
If it doesn't work, consider if

1. you have invisible ctrl chars,
2. has file been uploaded from MSWin and NOT had line endings converted (dos2unix)
3. is it a case sensitivity issue?
4. is that phrase split with a newline in the middle, grep/egrep default to treating each line separately (std unix approach in many tools)

Show part of the partial.eventlog contents

casperdaghost 08-24-2009 03:11 AM

hey everybody -

thank you for your posts. everyone here has been great.
the list above is a conglomeration of a few fruitless file searches.
lets just say that my organization has two matching engines - one department called me up and said 'where is 1/3 of my stuff' (note single quotes) and i was only picking up the other 2/3's. I spend alot of time digging through a 7 gig log.

anyhow it turns out that one of the channels on the circuit from our matching engine went down, and we did not have a record of certain transactions. all systems were up - just the one connection was down, networking reported it the next day.

grep with double or single quotes actually returned the correct output - just the info was wrong.

it was a networking issue, not a user issue.


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