[opensuse] Reg. expression
I'd like to go through a file and find all the lines with the word "LOG" in them. How do I do that? Like "^(*[LOG]*)$" or what...? /J -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 07 February 2007 14:38, Jan Karjalainen wrote:
I'd like to go through a file and find all the lines with the word "LOG" in them. How do I do that? Like "^(*[LOG]*)$" or what...?
grep LOG [filename] I think you're trying to make this more complicated than it is. Now if you want LOG to be a word by itself, then: grep ' LOG ' [filename] Of if it can be at the beginning or end of a line... grep '\bLOG\b' [filename] good luck, brian -- Brian Jackson Photo Sports ~ People ~ Events http://www.BrianJacksonPhoto.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Brian Jackson wrote:
On Wednesday 07 February 2007 14:38, Jan Karjalainen wrote:
I'd like to go through a file and find all the lines with the word "LOG" in them. How do I do that? Like "^(*[LOG]*)$" or what...?
grep LOG [filename]
I think you're trying to make this more complicated than it is. Now if you want LOG to be a word by itself, then:
grep ' LOG ' [filename]
Of if it can be at the beginning or end of a line...
grep '\bLOG\b' [filename]
good luck, brian
Thanks for the tip, but I knew about grep already. It's because of an application that has a search and replace function which supports regular expressions... /J -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jan, On Wednesday 07 February 2007 14:49, Jan Karjalainen wrote:
Brian Jackson wrote:
On Wednesday 07 February 2007 14:38, Jan Karjalainen wrote:
I'd like to go through a file and find all the lines with the word "LOG" in them. How do I do that? Like "^(*[LOG]*)$" or what...?
grep LOG [filename]
I think you're trying to make this more complicated than it is. Now if you want LOG to be a word by itself, then:
grep ' LOG ' [filename]
Of if it can be at the beginning or end of a line...
grep '\bLOG\b' [filename]
I usually just use the "-w" option when I want to find the pattern only as a full word (i.e., be bounded on each end by either a line beginning or end or by a non-alphanumeric character). % grep --help ... -w, --word-regexp force PATTERN to match only whole words ... Of course, the "\b" (and it's close relative, \B) is more flexible, giving you the ability to place the pattern at a word boundary on one end and not the other.
good luck, brian
Thanks for the tip, but I knew about grep already. It's because of an application that has a search and replace function which supports regular expressions...
Are you saying you need not simply to find the lines that contain "LOG" but also to apply some modification to such lines? Then you'll want "sed" or "awk" or possibly to use Perl. Can you clarify what you need to accomplish? Based on your original question, Brian's answer seems correct and appropriate.
/J
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
Jan,
On Wednesday 07 February 2007 14:49, Jan Karjalainen wrote:
Brian Jackson wrote:
On Wednesday 07 February 2007 14:38, Jan Karjalainen wrote:
I'd like to go through a file and find all the lines with the word "LOG" in them. How do I do that? Like "^(*[LOG]*)$" or what...?
grep LOG [filename]
I think you're trying to make this more complicated than it is. Now if you want LOG to be a word by itself, then:
grep ' LOG ' [filename]
Of if it can be at the beginning or end of a line...
grep '\bLOG\b' [filename]
I usually just use the "-w" option when I want to find the pattern only as a full word (i.e., be bounded on each end by either a line beginning or end or by a non-alphanumeric character).
% grep --help ... -w, --word-regexp force PATTERN to match only whole words ...
Of course, the "\b" (and it's close relative, \B) is more flexible, giving you the ability to place the pattern at a word boundary on one end and not the other.
good luck, brian
Thanks for the tip, but I knew about grep already. It's because of an application that has a search and replace function which supports regular expressions...
Are you saying you need not simply to find the lines that contain "LOG" but also to apply some modification to such lines? Then you'll want "sed" or "awk" or possibly to use Perl.
Can you clarify what you need to accomplish? Based on your original question, Brian's answer seems correct and appropriate.
I'm trying to parse through a file and delete all the lines with the word "LOG" on it, eg. replace it with "". -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 07 February 2007 15:19, Jan Karjalainen wrote:
I'm trying to parse through a file and delete all the lines with the word "LOG" on it, eg. replace it with "".
That's not the same thing as finding all lines with LOG. perl -pi -e 's/LOG//' [filename] (hey, wasn't this a thread last week? :-D) Wait, delete all lines with LOG or replace the string 'LOG' with ''? 2 different tasks. brian -- Brian Jackson Photo Sports ~ People ~ Events http://www.BrianJacksonPhoto.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Brian Jackson wrote:
On Wednesday 07 February 2007 15:19, Jan Karjalainen wrote:
I'm trying to parse through a file and delete all the lines with the word "LOG" on it, eg. replace it with "".
That's not the same thing as finding all lines with LOG.
perl -pi -e 's/LOG//' [filename]
(hey, wasn't this a thread last week? :-D)
Wait, delete all lines with LOG or replace the string 'LOG' with ''? 2 different tasks.
brian
I found the expression: .+LOG.+ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jan, On Wednesday 07 February 2007 15:30, Jan Karjalainen wrote:
...
On Wednesday 07 February 2007 15:19, Jan Karjalainen wrote:
I'm trying to parse through a file and delete all the lines with the word "LOG" on it, eg. replace it with "".
...
I found the expression: .+LOG.+
You're still making it too complicated for programs such as ed, vi, grep, egrep or sed, patterns are not required to match the whole line. That means the ".+" parts are redundant. If you really want to exclude from treatment those lines where "LOG" occurs at the beginning or end, then use the pattern ".LOG." (sans quotes, of course). Under some circumstances regular expression efficiency matters little, but given the size to which log files can grow, more efficient regular expressions are worth using. So here's what you want to do: % sed -e '/LOG/d' originalLogFile >filteredLogFile If you really want to exclude those lines where LOG occurs at the beginning or end, then use this: % sed -e '/.LOG./d' originalLogFile >filteredLogFile Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
Jan,
On Wednesday 07 February 2007 15:30, Jan Karjalainen wrote:
...
On Wednesday 07 February 2007 15:19, Jan Karjalainen wrote:
I'm trying to parse through a file and delete all the lines with the word "LOG" on it, eg. replace it with "".
...
I found the expression: .+LOG.+
You're still making it too complicated for programs such as ed, vi, grep, egrep or sed, patterns are not required to match the whole line. That means the ".+" parts are redundant. If you really want to exclude from treatment those lines where "LOG" occurs at the beginning or end, then use the pattern ".LOG." (sans quotes, of course).
Under some circumstances regular expression efficiency matters little, but given the size to which log files can grow, more efficient regular expressions are worth using.
So here's what you want to do:
% sed -e '/LOG/d' originalLogFile >filteredLogFile
If you really want to exclude those lines where LOG occurs at the beginning or end, then use this:
% sed -e '/.LOG./d' originalLogFile >filteredLogFile
Randall Schulz
The word LOG appears somewhere 2/3 in the line... -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Wed, 07 Feb 2007, Randall R Schulz wrote:
On Wednesday 07 February 2007 15:30, Jan Karjalainen wrote: [..]
I found the expression: .+LOG.+
You're still making it too complicated for programs such as ed, vi, grep, egrep or sed, patterns are not required to match the whole line. That means the ".+" parts are redundant. If you really want to exclude from treatment those lines where "LOG" occurs at the beginning or end, then use the pattern ".LOG." (sans quotes, of course).
That means 'one arbitrary character' "LOG" 'one arbitrary character', which is something else.
Under some circumstances regular expression efficiency matters little, but given the size to which log files can grow, more efficient regular expressions are worth using.
So here's what you want to do:
% sed -e '/LOG/d' originalLogFile >filteredLogFile
fgrep -v 'LOG' originalLogFile >filteredLogFile
If you really want to exclude those lines where LOG occurs at the beginning or end, then use this:
% sed -e '/.LOG./d' originalLogFile >filteredLogFile
fgrep -v '^LOG\|LOG$' originalLogFile >filteredLogFile -dnh -- Fertility is hereditary. If your parents didn't have any children, neither will you. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Thursday 08 February 2007 14:28, David Haller wrote:
Hello,
On Wed, 07 Feb 2007, Randall R Schulz wrote:
On Wednesday 07 February 2007 15:30, Jan Karjalainen wrote:
[..]
I found the expression: .+LOG.+
You're still making it too complicated for programs such as ed, vi, grep, egrep or sed, patterns are not required to match the whole line. That means the ".+" parts are redundant. If you really want to exclude from treatment those lines where "LOG" occurs at the beginning or end, then use the pattern ".LOG." (sans quotes, of course).
That means 'one arbitrary character' "LOG" 'one arbitrary character', which is something else.
How so? It will match LOG as long as it's not at the beginning or end of a line, which is what I said. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Brian Jackson
-
David Haller
-
Jan Karjalainen
-
Randall R Schulz