Unix Shell - finding a match within a line?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > finding a match within a line?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author finding a match within a line?
tony

2007-02-28, 1:22 am

Hi,

Sorry if this has been answered before, but I'm not sure if it how to
search for it in the posts:
Is there a unix program or script that extracts a match within a line
and not the result of the matched line itself?

Grep returns matches by lines but I want the actual match string
within the line.

where cat file returns

132. some text 76242005
133. more text 304275008 330275008
134. text 41056006

I want to find the long numbers and not [0-4]{3,}\.

grep -E '{[[:digit:]]{4,9}}' file
will give me the long numbers when I test this sample on a tool like
visual regexp http://laurent.riesterer.free.fr/regexp/
which is exactly the result needed, but I wonder if there is a script
to do the same thing or do I have to learn perl?

Basically what is the right tool for finding a match within a line?

Best,
tony

Passer By

2007-02-28, 1:22 am

"tony" <ccahua@gmail.com> writes:

> Hi,
>
> Sorry if this has been answered before, but I'm not sure if it how to
> search for it in the posts:
> Is there a unix program or script that extracts a match within a line
> and not the result of the matched line itself?
>
> Grep returns matches by lines but I want the actual match string
> within the line.
>
> where cat file returns
>
> 132. some text 76242005
> 133. more text 304275008 330275008
> 134. text 41056006
>
> I want to find the long numbers and not [0-4]{3,}\.
>
> grep -E '{[[:digit:]]{4,9}}' file
> will give me the long numbers when I test this sample on a tool like
> visual regexp http://laurent.riesterer.free.fr/regexp/
> which is exactly the result needed, but I wonder if there is a script
> to do the same thing or do I have to learn perl?
>
> Basically what is the right tool for finding a match within a line?
>
> Best,
> tony


Seems like you want

% echo "123 just this 567" | grep -o "just this"
just this
% echo "134. text 41056006" | grep -o '[0-9]\{4,9\}'
41056006
%


Xicheng Jia

2007-02-28, 1:22 am

On Feb 27, 10:49 pm, "tony" <cca...@gmail.com> wrote:
> Hi,
>
> Sorry if this has been answered before, but I'm not sure if it how to
> search for it in the posts:
> Is there a unix program or script that extracts a match within a line
> and not the result of the matched line itself?
>
> Grep returns matches by lines but I want the actual match string
> within the line.
>
> where cat file returns
>
> 132. some text 76242005
> 133. more text 304275008 330275008
> 134. text 41056006
>
> I want to find the long numbers and not [0-4]{3,}\.
>
> grep -E '{[[:digit:]]{4,9}}' file
> will give me the long numbers when I test this sample on a tool like
> visual regexp http://laurent.riesterer.free.fr/regexp/
> which is exactly the result needed, but I wonder if there is a script
> to do the same thing or do I have to learn perl?
>
> Basically what is the right tool for finding a match within a line?
>


1) if your grep has -o option, then use it

2) use perl, it's also easy:

perl -lne 'print for /\d{4,9}/g' file.txt

BTW. you may want to add boundary anchors to your pattern to filter
out long strings (more than 9 digits like 111111111111111111111
though :-))

Regards,
Xicheng

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com