[H-GEN] char inset
jasonp at uq.net.au
jasonp at uq.net.au
Tue May 2 21:27:45 EDT 2000
On Wed, May 03, 2000 at 10:37:54AM +1000, Bruce Campbell wrote:
> bash:
> l1="`echo $input | cut -c -1`"
> l2="`echo $input | cut -c 2-`"
> output="${l1}0${l2}"
> perl:
> $output = $input;
> $output =~ /^([A-Za-z])(\d\d)$/${1}0${2}/g;
>
> Both are doing the same thing. Use perl, as the regex is easier.
Um. (Bruce, why are you using a /g modifier?)
perl -pe's/([a-zA-Z])/${1}0/'
B43
B043
C82
C082
Of course that will not work for *all* input, but since the original
poster didn't give more than one example, it's hard to guess what the
intent was. The lesson to take away is that you don't need to match
the *whole* string---just the bit you're interested in. Indeed Bruce's
script will break if there are more than two digits at the end of the
string. My example will break if there is more than one alphabetic in
the string. It's all about knowing the limitations of what you write.
Make sure you take backups and test your scripting thoroughly.
Alternatively, if you want to simply insert some number immediately
following the first character:
perl -pe'substr $_, 1, 0, "0"'
B43
B043
C09
C009
(This script inserts the string "0" at position 1 (the second character)
and replaces the next 0 characters with it. The -p argument says `do
this, then print $_'.)
jason
--
The Patently Ridiculous:
http://www.patents.ibm.com/details?pn=US06025810__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
URL: <http://lists.humbug.org.au/pipermail/general/attachments/20000503/57674eeb/attachment.sig>
More information about the General
mailing list