LinuxQuestions.org
Review your favorite Linux distribution.
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 08-22-2009, 06:18 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 346

Rep: Reputation: 16
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>

Last edited by casperdaghost; 08-22-2009 at 06:19 AM.
 
Old 08-22-2009, 06:34 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
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

Last edited by markush; 08-22-2009 at 06:37 AM.
 
Old 08-22-2009, 06:42 AM   #3
SeSoX
LQ Newbie
 
Registered: Aug 2009
Posts: 18

Rep: Reputation: 0
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.
 
Old 08-22-2009, 07:00 AM   #4
andywebsdale
Member
 
Registered: Jan 2005
Location: Lewisham,London,UK
Distribution: Debian Wheezy AMD64
Posts: 87
Blog Entries: 2

Rep: Reputation: 23
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

Last edited by andywebsdale; 08-22-2009 at 07:02 AM. Reason: addition
 
Old 08-22-2009, 08:29 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by andywebsdale View Post
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!
 
Old 08-22-2009, 08:38 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hello casperdaghost

Is the screen session you quote exactly what appeared on your screen?
Quote:
Originally Posted by casperdaghost View Post

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)
 
Old 08-23-2009, 08:29 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 08-24-2009, 02:11 AM   #8
casperdaghost
Member
 
Registered: Aug 2009
Posts: 346

Original Poster
Rep: Reputation: 16
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.
 
  


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
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 06:29 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
find word between string djcham Programming 8 07-11-2008 04:37 AM
Read a word in a string with bash orgazmo Programming 6 06-07-2005 10:19 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration