Grep and return only text in between two points

  • Follow


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:













7/21/2012 5:40:44 PM


Reply: