Trim up to a character sequence (sed?)
Trim up to a character sequence (sed?)
am 06.10.2007 18:59:07 von ohaya
Hi,
When displaying lines from some log files, I'd like to be able to trim
all the characters on each line that precede a specific character sequence.
For example, the log line might look like:
xxxxxxxxxx+++++++++yyyyyyyyyyyyyyyy...
and I'd like to be able to trim the "xxx...xxx" in front of the
"++++...+++" when I do:
cat logfilename | grep "+++++++"
or
grep "++++++" logfilename
Can this be done by piping the output to sed?
If so, can anyone tell me how?
Thanks,
Jim
Re: Trim up to a character sequence (sed?)
am 06.10.2007 19:47:24 von Icarus Sparry
On Sat, 06 Oct 2007 12:59:07 -0400, ohaya wrote:
> Hi,
>
> When displaying lines from some log files, I'd like to be able to trim
> all the characters on each line that precede a specific character
> sequence.
>
> For example, the log line might look like:
>
> xxxxxxxxxx+++++++++yyyyyyyyyyyyyyyy...
>
> and I'd like to be able to trim the "xxx...xxx" in front of the
> "++++...+++" when I do:
>
> cat logfilename | grep "+++++++"
> or
> grep "++++++" logfilename
>
> Can this be done by piping the output to sed?
>
> If so, can anyone tell me how?
>
> Thanks,
> Jim
sed 's/.*\(++++++++\)/\1/' logfile
or maybe
sed -n 's/.*\(++++++++\)/\1/p' logfile
if you want the effect of grep in there as well.
The sed command is of the form s{delimiter}{pattern1}{delimiter}{pattern2}
{delimiter} with the delimiter being slash. Slash is a fairly
conventional choice provided that it does not appear in either of the
patterns.
pattern1 is .*\(+++++++\)
A dot matches any character aprt from newline. The * modifies the
previous thing to say "zero or more", so .* matches any number of
characters. The \( and \) start a group to be remembered. The ++++++++
match that pattern exactly.
pattern2 is \1
This is the characters that matched the first group.
If you add the "-n" flag then it tels sed not to print the lines by
default. If you add the p after the third delimiter it tells it to print
the line if if made a modification to the line.
Re: Trim up to a character sequence (sed?)
am 06.10.2007 19:57:50 von Cyrus Kriticos
ohaya wrote:
>
> When displaying lines from some log files, I'd like to be able to trim
> all the characters on each line that precede a specific character sequence.
>
> For example, the log line might look like:
>
> xxxxxxxxxx+++++++++yyyyyyyyyyyyyyyy...
>
> and I'd like to be able to trim the "xxx...xxx" in front of the
> "++++...+++" when I do:
>
> cat logfilename | grep "+++++++"
> or
> grep "++++++" logfilename
>
> Can this be done by piping the output to sed?
yes
> If so, can anyone tell me how?
sed 's/^xxxxxxxxxx//' logfile
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Trim up to a character sequence (sed?)
am 07.10.2007 02:10:02 von ohaya
Icarus Sparry wrote:
> On Sat, 06 Oct 2007 12:59:07 -0400, ohaya wrote:
>
>> Hi,
>>
>> When displaying lines from some log files, I'd like to be able to trim
>> all the characters on each line that precede a specific character
>> sequence.
>>
>> For example, the log line might look like:
>>
>> xxxxxxxxxx+++++++++yyyyyyyyyyyyyyyy...
>>
>> and I'd like to be able to trim the "xxx...xxx" in front of the
>> "++++...+++" when I do:
>>
>> cat logfilename | grep "+++++++"
>> or
>> grep "++++++" logfilename
>>
>> Can this be done by piping the output to sed?
>>
>> If so, can anyone tell me how?
>>
>> Thanks,
>> Jim
>
> sed 's/.*\(++++++++\)/\1/' logfile
> or maybe
>
> sed -n 's/.*\(++++++++\)/\1/p' logfile
> if you want the effect of grep in there as well.
>
> The sed command is of the form s{delimiter}{pattern1}{delimiter}{pattern2}
> {delimiter} with the delimiter being slash. Slash is a fairly
> conventional choice provided that it does not appear in either of the
> patterns.
>
> pattern1 is .*\(+++++++\)
> A dot matches any character aprt from newline. The * modifies the
> previous thing to say "zero or more", so .* matches any number of
> characters. The \( and \) start a group to be remembered. The ++++++++
> match that pattern exactly.
>
> pattern2 is \1
> This is the characters that matched the first group.
>
> If you add the "-n" flag then it tels sed not to print the lines by
> default. If you add the p after the third delimiter it tells it to print
> the line if if made a modification to the line.
Icarus and Cyrus,
I think that I didn't explain myself clearly in my original post. I
don't know what is in the beginning part of the lines (they vary), but I
just wanted to see what came after the "++++...++".
The 2nd line from Icarus' post kind of worked, but not by itself. When
run by itself, nothing happen.
So, I modified Icarus' 2nd example, and instead of just the sed, I did
the following (in a shell script):
grep "++++++" $1 | sed -n 's/.*\(+++++\)//p'
So, for example, a log line (this is from a WebLogic log) might look like:
####
<[ACTIVE] ExecuteThread: '3' for queue: '
weblogic.kernel.Default (self-tuning)'> <>
<> <1191640875143> <000000> <++++++++++
GetLdapAttributeImpl:onAcquire:>
When run through the above, it displays as:
GetLdapAttributeImpl:onAcquire:>
This makes things a lot more readable :)!!
Thanks!
Jim
Re: Trim up to a character sequence (sed?)
am 07.10.2007 03:12:14 von Janis Papanagnou
ohaya wrote:
> Icarus Sparry wrote:
>
>> On Sat, 06 Oct 2007 12:59:07 -0400, ohaya wrote:
>>
>>> Hi,
>>>
>>> When displaying lines from some log files, I'd like to be able to trim
>>> all the characters on each line that precede a specific character
>>> sequence.
>>>
>>> For example, the log line might look like:
>>>
>>> xxxxxxxxxx+++++++++yyyyyyyyyyyyyyyy...
>>>
>>> and I'd like to be able to trim the "xxx...xxx" in front of the
>>> "++++...+++" when I do:
>>>
>>> cat logfilename | grep "+++++++"
>>> or
>>> grep "++++++" logfilename
>>>
>>> Can this be done by piping the output to sed?
>>>
>>> If so, can anyone tell me how?
>>>
>>> Thanks,
>>> Jim
>>
>>
>> sed 's/.*\(++++++++\)/\1/' logfile
>> or maybe
>>
>> sed -n 's/.*\(++++++++\)/\1/p' logfile
>> if you want the effect of grep in there as well.
>>
>> The sed command is of the form
>> s{delimiter}{pattern1}{delimiter}{pattern2}
>> {delimiter} with the delimiter being slash. Slash is a fairly
>> conventional choice provided that it does not appear in either of the
>> patterns.
>>
>> pattern1 is .*\(+++++++\)
>> A dot matches any character aprt from newline. The * modifies the
>> previous thing to say "zero or more", so .* matches any number of
>> characters. The \( and \) start a group to be remembered. The ++++++++
>> match that pattern exactly.
>>
>> pattern2 is \1
>> This is the characters that matched the first group.
>>
>> If you add the "-n" flag then it tels sed not to print the lines by
>> default. If you add the p after the third delimiter it tells it to
>> print the line if if made a modification to the line.
>
>
> Icarus and Cyrus,
>
> I think that I didn't explain myself clearly in my original post. I
> don't know what is in the beginning part of the lines (they vary), but I
> just wanted to see what came after the "++++...++".
>
> The 2nd line from Icarus' post kind of worked, but not by itself. When
> run by itself, nothing happen.
>
> So, I modified Icarus' 2nd example, and instead of just the sed, I did
> the following (in a shell script):
>
> grep "++++++" $1 | sed -n 's/.*\(+++++\)//p'
Why not...
sed -n 's/.*+++++//p' "$1"
Janis
>
> So, for example, a log line (this is from a WebLogic log) might look like:
>
> ####
> <[ACTIVE] ExecuteThread: '3' for queue: '
> weblogic.kernel.Default (self-tuning)'> <>
> <> <1191640875143> <000000> <++++++++++
> GetLdapAttributeImpl:onAcquire:>
>
> When run through the above, it displays as:
>
> GetLdapAttributeImpl:onAcquire:>
>
> This makes things a lot more readable :)!!
>
> Thanks!
>
> Jim