Extract the substring between two different tokens.

Extract the substring between two different tokens.

am 24.10.2007 14:42:20 von Khan

Hi,

Please let me know the code(script), to extract a substring between
two different tokens.

Ex: I/P: "UDP:[192.168.2.143]:6752"
>From this input, i want extact only the IPaddress .i.e.
O/P: 192.168.2.143.

Thanks
-Khan

Re: Extract the substring between two different tokens.

am 24.10.2007 14:59:46 von Ed Morton

khan wrote:
> Hi,
>
> Please let me know the code(script), to extract a substring between
> two different tokens.
>
> Ex: I/P: "UDP:[192.168.2.143]:6752"
>>From this input, i want extact only the IPaddress .i.e.
> O/P: 192.168.2.143.
>
> Thanks
> -Khan
>

If you can only have one of those patterns per line and it it's never
split across lines:

sed 's/.*\[\(.*\)\].*/\1/' file

If it's something else, tell us...

Ed.

Re: Extract the substring between two different tokens.

am 24.10.2007 17:07:57 von Stephane CHAZELAS

2007-10-24, 05:42(-07), khan:
> Hi,
>
> Please let me know the code(script), to extract a substring between
> two different tokens.
>
> Ex: I/P: "UDP:[192.168.2.143]:6752"
>>From this input, i want extact only the IPaddress .i.e.
> O/P: 192.168.2.143.


IP="UDP:[192.168.2.143]:6752"
expr "x$IP" : 'xUDP:\[\(.*\)\]:'

--
Stéphane

Re: Extract the substring between two different tokens.

am 24.10.2007 17:44:07 von Michael Tosch

Stephane CHAZELAS wrote:
> 2007-10-24, 05:42(-07), khan:
>> Hi,
>>
>> Please let me know the code(script), to extract a substring between
>> two different tokens.
>>
>> Ex: I/P: "UDP:[192.168.2.143]:6752"
>> >From this input, i want extact only the IPaddress .i.e.
>> O/P: 192.168.2.143.
>
>
> IP="UDP:[192.168.2.143]:6752"
> expr "x$IP" : 'xUDP:\[\(.*\)\]:'
>

In case additional [] can follow,
it is often better to do a minimal match:

\(.*\)
should be
\([^]]*\)

echo "$IP" | sed 's/.*\[\([^]]*\)\].*/\1/'
expr x"$IP" : '.*\[\([^]]*\)\]'

--
Michael Tosch @ hp : com

Re: Extract the substring between two different tokens.

am 25.10.2007 07:18:56 von ramesh.thangamani

On Oct 24, 8:44 pm, Michael Tosch
wrote:
> Stephane CHAZELAS wrote:
> > 2007-10-24, 05:42(-07), khan:
> >> Hi,
>
> >> Please let me know the code(script), to extract a substring between
> >> two different tokens.
>
> >> Ex: I/P: "UDP:[192.168.2.143]:6752"
> >> >From this input, i want extact only the IPaddress .i.e.
> >> O/P: 192.168.2.143.
>
> > IP="UDP:[192.168.2.143]:6752"
> > expr "x$IP" : 'xUDP:\[\(.*\)\]:'
>
> In case additional [] can follow,
> it is often better to do a minimal match:
>
> \(.*\)
> should be
> \([^]]*\)
>
> echo "$IP" | sed 's/.*\[\([^]]*\)\].*/\1/'
> expr x"$IP" : '.*\[\([^]]*\)\]'
>
> --
> Michael Tosch @ hp : com

Using perl:

perl -e 'my $str="UDP:[192.168.2.143]:6752"; $str =~ /\[(.*)\]/; print
$1;'