PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Splitting a string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Splitting a string

Réponse
 
LinkBack Outils de la discussion
Vieux 07/12/2006, 13h07   #1
Antonio Maschio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Splitting a string

Hi,

suppose I have a string composed by a number and some characters (e.g. a
BASIC statement like

10 PRINT I

that could be also written as

10PRINT I
10 <spaces> PRINT I
10 <tabs> PRINT I
10 <spaces and tabs> PRINT I

or something like this); how can I separate in bash the number from the
characters, splitting the string so that, for instance,

ln=10
pr="PRINT I" or whatever, discarding also all the trailing blanks after
last useful character of "PRINT I"? ln should be any integer number, and
it could be 1 (one) or 15678, for example. Can you me?

I use bash 3.x, and I'm not really a guru (though not a newbie).

Thanks in advance

-- Antonio
  Réponse avec citation
Vieux 07/12/2006, 16h05   #2
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

Antonio Maschio wrote:
> Hi,
>
> suppose I have a string composed by a number and some characters (e.g. a
> BASIC statement like
>
> 10 PRINT I
>
> that could be also written as
>
> 10PRINT I
> 10 <spaces> PRINT I
> 10 <tabs> PRINT I
> 10 <spaces and tabs> PRINT I
>
> or something like this); how can I separate in bash the number from the
> characters, splitting the string so that, for instance,
>
> ln=10
> pr="PRINT I" or whatever, discarding also all the trailing blanks after
> last useful character of "PRINT I"? ln should be any integer number, and
> it could be 1 (one) or 15678, for example. Can you me?
>
> I use bash 3.x, and I'm not really a guru (though not a newbie).
>
> Thanks in advance
>
> -- Antonio


Is the Basic command in a file or in a variable?
If you read the line from file...

read -r line
ln=${line%%[!0-9]*}
cmd=${line##*[0-9]}
echo "$ln"
echo "$cmd"


Janis

  Réponse avec citation
Vieux 07/12/2006, 19h43   #3
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

On Thu, 07 Dec 2006 08:05:11 -0800, Janis wrote:

> Antonio Maschio wrote:
>> Hi,
>>
>> suppose I have a string composed by a number and some characters (e.g. a
>> BASIC statement like
>>
>> 10 PRINT I
>>
>> that could be also written as
>>
>> 10PRINT I
>> 10 <spaces> PRINT I
>> 10 <tabs> PRINT I
>> 10 <spaces and tabs> PRINT I
>>
>> or something like this); how can I separate in bash the number from the
>> characters, splitting the string so that, for instance,
>>
>> ln=10
>> pr="PRINT I" or whatever, discarding also all the trailing blanks after
>> last useful character of "PRINT I"? ln should be any integer number, and
>> it could be 1 (one) or 15678, for example. Can you me?
>>
>> I use bash 3.x, and I'm not really a guru (though not a newbie).
>>
>> Thanks in advance
>>
>> -- Antonio

>
> Is the Basic command in a file or in a variable?
> If you read the line from file...
>
> read -r line
> ln=${line%%[!0-9]*}
> cmd=${line##*[0-9]}
> echo "$ln"
> echo "$cmd"
>
>
> Janis


The line number extraction is fine provided there are no initial spaces,
but the cmd extraction fails say for the example

10 print 42

Try

cmd=${line#${ln}}
if there are no spaces at the start of the line. cmd will contain initial
spaces if there are any after the line number. Since you are using bash 3,
you can also use extended glob patterns. These are

# Enable extended patterns
shopt -s extglob
# like sed 's/^ *[0-9][0-9]* *//'
cmd=${line/#*( )+([0-9])*( )/}
# like sed 's/^ *//'
ln=${line##*( )}
# like sed 's/[!0-9].*//'
ln=${ln%%[!0-9]*}
  Réponse avec citation
Vieux 07/12/2006, 22h08   #4
scott_layne_gillespie@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

sed 's/\([0-9]*\)[a-zA-Z ]/\1/g' < inputfile

or

cat inputfile | sed 's/\([0-9]*\)[a-zA-Z ]/\1/g'
|
this is a space followed
by a tab

where there is a space and a tab after the uppercase Z in A-Z

hth

Scott Gillespie

Antonio Maschio wrote:
> Hi,
>
> suppose I have a string composed by a number and some characters (e.g. a
> BASIC statement like
>
> 10 PRINT I
>
> that could be also written as
>
> 10PRINT I
> 10 <spaces> PRINT I
> 10 <tabs> PRINT I
> 10 <spaces and tabs> PRINT I
>
> or something like this); how can I separate in bash the number from the
> characters, splitting the string so that, for instance,
>
> ln=10
> pr="PRINT I" or whatever, discarding also all the trailing blanks after
> last useful character of "PRINT I"? ln should be any integer number, and
> it could be 1 (one) or 15678, for example. Can you me?
>
> I use bash 3.x, and I'm not really a guru (though not a newbie).
>
> Thanks in advance
>
> -- Antonio


  Réponse avec citation
Vieux 07/12/2006, 23h53   #5
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

Icarus Sparry wrote:
> On Thu, 07 Dec 2006 08:05:11 -0800, Janis wrote:
>>
>>read -r line
>>ln=${line%%[!0-9]*}
>>cmd=${line##*[0-9]}
>>echo "$ln"
>>echo "$cmd"
>>

>
> The line number extraction is fine provided there are no initial spaces,
> but the cmd extraction fails say for the example


Yes, you're right. I had the ksh syntax

cmd=${line##*([0-9])}

in mind (which wouldn't be available in bash anyway).
I shouldn't post while in a hurry.
Thank's for the correction.

Janis
  Réponse avec citation
Vieux 08/12/2006, 00h03   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

2006-12-08, 00:53(+01), Janis Papanagnou:
[...]
>> The line number extraction is fine provided there are no initial spaces,
>> but the cmd extraction fails say for the example

>
> Yes, you're right. I had the ksh syntax
>
> cmd=${line##*([0-9])}
>
> in mind (which wouldn't be available in bash anyway).

[...]

The ksh88 syntax is available in bash after a:

shopt -s extglob

and in zsh after

setopt kshglob

In zsh, you may prefer zsh's extendedglob

cmd=${line##[0-9]#}

or

cmd=${line##<0->}

(<0-> is any decimal number starting from 0).

Newer ksh93 operators such as {1,2}(...), %(...) are not
available yet in bash or zsh.

--
Stéphane
  Réponse avec citation
Vieux 08/12/2006, 10h20   #7
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Splitting a string

Stephane CHAZELAS wrote:
> 2006-12-08, 00:53(+01), Janis Papanagnou:
> >
> > cmd=${line##*([0-9])}

>
> The ksh88 syntax is available in bash after a:
>
> shopt -s extglob


Ah, yes; I had tried it just in plain bash.
Why don't they activate that feature by default?

Janis

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16727 seconds with 15 queries