|
|
Grep and return only text in between two points
Here's a weird one on my learning shell scripting adventures. I'd
like to be able to cat a log file and only return a piece of text
between two points.
Example: "STRING: @out TCP from 192.168.0.1 to
www.site.com(192.168.0.2"
I would like to have the grep only return the piece "192.168.0.1 to
www.site.com"
I'm not sure what the best method of doing this is, whether it be with
grep, egrep, awk or any of that stuff. I'm still very new to the
whole controlling of text. Any suggestions would be awesome!
-Sys
|
|
0
|
|
|
|
Reply
|
sysera
|
2/27/2004 11:55:09 AM |
|
2004-02-27, 03:55(-08), sysera:
> Here's a weird one on my learning shell scripting adventures. I'd
> like to be able to cat a log file and only return a piece of text
> between two points.
>
> Example: "STRING: @out TCP from 192.168.0.1 to
> www.site.com(192.168.0.2"
>
> I would like to have the grep only return the piece "192.168.0.1 to
> www.site.com"
[...]
You could only do it with a version of GNU grep with pcre
support:
grep -Po '(?<=^STRING: @out TCP from )[^(]*'
(untested)
Best is to use sed:
sed -n 's/^STRING: @out TCP from \([^(]*\).*/\1/p'
--
St�phane ["Stephane.Chazelas" at "free.fr"]
|
|
0
|
|
|
|
Reply
|
Stephane
|
2/27/2004 12:48:17 PM
|
|
On 27 Feb 2004 03:55:09 -0800, sysera <sysera@themna.com> wrote:
>
>
> Here's a weird one on my learning shell scripting adventures. I'd
> like to be able to cat a log file and only return a piece of text
> between two points.
>
> Example: "STRING: @out TCP from 192.168.0.1 to
> www.site.com(192.168.0.2"
>
> I would like to have the grep only return the piece "192.168.0.1 to
> www.site.com"
>
> I'm not sure what the best method of doing this is, whether it be with
> grep, egrep, awk or any of that stuff. I'm still very new to the
> whole controlling of text. Any suggestions would be awesome!
>
> -Sys
You could do:
cat logfile | sed -n'/pattern1/,/pattern2/p' > outputfile
The patterns need to be regular expressions.
www\.site\.com(192\.
and so forth.
Hope this helps.
(I'm assuming that each pattern is on a different line, that you want a
block of text from the file.)
Just coincidentally, learning to use ed is good way to learn basic regular
expressions and a good introduction to sed and grep.
(and useful in scripts itself, because it doesn't need a temporary file)
grep is an ed command:
g/RE/p
g means search every line in the file: global
RE is a regular expression, the "patterns" above
and p means print what it finds, as it does above.
I'm using ed here. Has helped my typing immensely.
AC
--
ed(1) Check out the original tutorials by Brian W.
Kernighan at the Ed Home Page http://tinyurl.com/2aa6g
|
|
0
|
|
|
|
Reply
|
Alan
|
2/27/2004 12:58:20 PM
|
|
Hi sys,
grep/egrep/fgrep can't handle that, awk or sed can, below sample using sed:
assuming your are looking for the following <pattern> on a line:
...... from <IPaddress ...... website>( ....
"from " and "(" are delimiters enclosing the <pattern>
the command:
cat yourfile | sed 's/^.*from
\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*.*\)(.*$/\1/'
Philippe
sysera wrote:
>Here's a weird one on my learning shell scripting adventures. I'd
>like to be able to cat a log file and only return a piece of text
>between two points.
>
>Example: "STRING: @out TCP from 192.168.0.1 to
>www.site.com(192.168.0.2"
>
>I would like to have the grep only return the piece "192.168.0.1 to
>www.site.com"
>
>I'm not sure what the best method of doing this is, whether it be with
>grep, egrep, awk or any of that stuff. I'm still very new to the
>whole controlling of text. Any suggestions would be awesome!
>
>-Sys
>
>
--
Philippe Scelers
|
|
0
|
|
|
|
Reply
|
Philippe
|
2/27/2004 1:16:02 PM
|
|
|
3 Replies
1124 Views
(page loaded in 0.12 seconds)
Similiar Articles: Grep and return only text in between two points - comp.unix.shell ...Here's a weird one on my learning shell scripting adventures. I'd like to be able to cat a log file and only return a piece of text between two point... Difference between grep, egrep and fgrep - comp.unix.programmer ...Grep and return only text in between two points - comp.unix.shell ... Difference between grep, egrep and fgrep - comp.unix.programmer ..... tell me what the differences ... Finding common lines between text files - comp.unix.programmer ...Grep and return only text in between two points - comp.unix.shell ... I'd like to be able to cat a log file and only return a piece of text between two ... that each ... How to use grep function to search exact word in HP UNIX 11.0 ...Grep and return only text in between two points - comp.unix.shell ..... like to have the grep only return the piece "192.168.0.1 to ... out to ... DIFF command - comp.unix.programmerGrep and return only text in between two points - comp.unix.shell ... DIFF command - comp.unix.programmer I am using the DIFF command to compare two text files. ... The difference between return and exit()? - comp.unix.programmer ...Grep and return only text in between two points - comp.unix.shell ... Difference between grep, egrep and fgrep - comp.unix.programmer ..... tell me what the differences ... sed: printing only between {} - comp.unix.shellGrep and return only text in between two points - comp.unix.shell ... Hi sys, grep/egrep/fgrep can't handle that, awk or sed can, below sample ... Delete line above and below the line which matches a pattern ...Grep and return only text in between two points - comp.unix.shell ... (I'm assuming that each pattern is on a different line, that you ... print what it finds, as it does ... Stationary point of curve - comp.soft-sys.matlabDIFF command - comp.unix.programmer Grep and return only text in between two points - comp.unix.shell ... DIFF command - comp.unix.programmer I am using the DIFF command ... Nehe demos use 99% cpu? - comp.graphics.api.openglGrep and return only text in between two points - comp.unix.shell ... The CPU only works ... need 99% of the interpolated points this method returns). ... > > Regards ... Grep and return only text in between two points - comp.unix.shell ...Here's a weird one on my learning shell scripting adventures. I'd like to be able to cat a log file and only return a piece of text between two point... Linux and UNIX grep command help - Computer Hope's free computer helpIf TYPE is text, grep processes a binary file as if it ... to list files in a bare format, use grep to filter only the ... When done properly the terminal should return a ... 7/21/2012 5:40:44 PM
|
|
|
|
|
|
|
|
|