Re: HowTo : Extract word with 'sed'

2002-08-11 Thread John H Darrah

On Fri, 9 Aug 2002, Paul Branston wrote:

> On Thu, Aug 08, 2002 at 11:40:32AM -0400, Michael C Tiernan wrote:
> > On Thursday 08 August 2002 08:59, Nick Lindsell said:
> >  > At 14:34 08/08/2002 +0200, you wrote:
> >  > > "File_21_05082002" and i would like to
> >  > > extract "21" from this.
> >  > >How can i do it with 'sed'?
> >  >
> >  > No need for sed, cut is simpler:-
> >  > $extract= echo "File_21_05082002"|cut -c 6-7
> >
> > Cut is easier for this problem but I'd modify it one bit
> > $extract= `echo "File_21_05082002"|cut -d_ -f2`
> >
> > This assumes (*cough*) that you want the value between the two "_" characters.
> >
> > To answer your original question.  In sed:
> > $extract=`echo $Fname | sed -e "s/^File_//" -e "s/_[0-9]*$//"`
> >
> > Should come very close to what you wanted (I think...)
>
> or in sed using 1 pattern match assuming File_ always comes before the
> digits and the end of the digits is a _ character.
>
>  echo "File_21_05082002"|sed -e 's/^File_\(.*\)_.*$/\1/'
>

Or just use shell globbing:

  orig="File_21_05082002"
  temp=${orig#*_}
  result=${temp%_*}
  echo $result

-- 
John Darrah (u05192)| Dept: N/C Programming
Giddens Industries  | Ph: (425) 353-0405 #133
PO box 3190 | Ph: (206) 767-4212 #133
Everett  WA98203| Fx: (206) 764-9639



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list



Re: HowTo : Extract word with 'sed'

2002-08-08 Thread Paul Branston

On Thu, Aug 08, 2002 at 11:40:32AM -0400, Michael C Tiernan wrote:
> On Thursday 08 August 2002 08:59, Nick Lindsell said:
>  > At 14:34 08/08/2002 +0200, you wrote:
>  > > "File_21_05082002" and i would like to
>  > > extract "21" from this.
>  > >How can i do it with 'sed'?
>  >
>  > No need for sed, cut is simpler:-
>  > $extract= echo "File_21_05082002"|cut -c 6-7
> 
> Cut is easier for this problem but I'd modify it one bit
> $extract= `echo "File_21_05082002"|cut -d_ -f2`
> 
> This assumes (*cough*) that you want the value between the two "_" characters.
> 
> To answer your original question.  In sed:
> $extract=`echo $Fname | sed -e "s/^File_//" -e "s/_[0-9]*$//"`
> 
> Should come very close to what you wanted (I think...)

or in sed using 1 pattern match assuming File_ always comes before the
digits and the end of the digits is a _ character.

 echo "File_21_05082002"|sed -e 's/^File_\(.*\)_.*$/\1/'



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list



Re: HowTo : Extract word with 'sed'

2002-08-08 Thread Ed Wilts

On Thu, Aug 08, 2002 at 01:59:50PM +0100, Nick Lindsell wrote:
> At 14:34 08/08/2002 +0200, you wrote:
> 
> >   I have a word which is :"File_21_05082002" and i would like to 
> > extract "21" from this.
> >
> >How can i do it with 'sed'?
> 
> No need for sed, cut is simpler:-
> 
> $extract= echo "File_21_05082002"|cut -c 6-7

cut is simpler, but if the format is always word_digits_word, then
cutting based on exact character count might not be best solution.  You
can also cut based on a delimiter.  Try (in bash):
extract=`echo "File_21_05082002"|cut -d _ -f 2`
This cuts the 2nd field where the string is delimited by underscores.

-- 
Ed Wilts, Mounds View, MN, USA
mailto:[EMAIL PROTECTED]
Member #1, Red Hat Community Ambassador Program



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list



Re: HowTo : Extract word with 'sed'

2002-08-08 Thread Michael C Tiernan

On Thursday 08 August 2002 08:59, Nick Lindsell said:
 > At 14:34 08/08/2002 +0200, you wrote:
 > > "File_21_05082002" and i would like to
 > > extract "21" from this.
 > >How can i do it with 'sed'?
 >
 > No need for sed, cut is simpler:-
 > $extract= echo "File_21_05082002"|cut -c 6-7

Cut is easier for this problem but I'd modify it one bit
$extract= `echo "File_21_05082002"|cut -d_ -f2`

This assumes (*cough*) that you want the value between the two "_" characters.

To answer your original question.  In sed:
$extract=`echo $Fname | sed -e "s/^File_//" -e "s/_[0-9]*$//"`

Should come very close to what you wanted (I think...)



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list