|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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]*} |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|