Re: extracting a particular pattern form a string
From: Bill Seivert (seivert_at_pcisys.net)
Date: 02/26/05
- Next message: Bill Seivert: "Re: makefile and TMPDIR interaction"
- Previous message: Mark Rafn: "Re: makefile and TMPDIR interaction"
- In reply to: junky_fellow_at_yahoo.co.in: "Re: extracting a particular pattern form a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 25 Feb 2005 20:29:39 -0700
junky_fellow@yahoo.co.in wrote:
> David Schwartz wrote:
>
>><junky_fellow@yahoo.co.in> wrote in message
>>news:1109328831.135767.34090@z14g2000cwz.googlegroups.com...
>>
>>
>>>I want to extract a particular pattern form a string using a shell
>>>command.
>>>
>>>Eg. I have a shell (ksh) variable VAR=PQRXYZ12ABCD
>>>
>>>I want to extract "12" from the shell variable VAR using some
>>>command.
>>>
>>>Thanx in advance for any help ...
>>
>> There are as many different ways to do this as there are people
>
> who
>
>>might want to do it. One way would be to use 'tr' to change every
>
> letter to
>
>>a newline and then use 'grep' to extract lines with digits in them.
>
> It
>
>>really depends upon exactly what you're trying to do. For example,
>
> whould
>
>>should happen to "a1b2c"?
>>
>> DS
>
>
> Ok. I think i did not put my question correctly. What i want is first
> I want to extract the substring "YZ12ABCD" (ie substring that start
> from YZ).
> Then I want to cut "12" from this string (ie want to cut 2nd and 3rd
> character)
> from the string.
>
expr will do this easily:
VAR=PQRXYZ12ABCD
yz=`expr "$VAR" : '.*\(YZ.*\)'`
twelve=`expr "$yz" : '..\(..\)'`
If you want the digits in yz, use
digits=`expr "$yz" : '[^0-9]*\([0-9][0-9]*\)'`
Bill Seivert
- Next message: Bill Seivert: "Re: makefile and TMPDIR interaction"
- Previous message: Mark Rafn: "Re: makefile and TMPDIR interaction"
- In reply to: junky_fellow_at_yahoo.co.in: "Re: extracting a particular pattern form a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]