Re: show only numbers before, within or after the text



Luca Villa wrote:
I have a long text file like this:

324yellow
34house
black
54532
15m21red56
44dfdsf8sfd23

How can I obtain (Perl - Windows/commandline/singleline) the
following?

1) only the numbers at the beginning before some alpha text, like
this:

324
34
15
44

2) only the numbers within the alpha text, like this:

21
8

3) only the numbers at the end after some alpha text, like this:

56
23

$ perl -le'
my @x = qw(
324yellow
34house
black
54532
15m21red56
44dfdsf8sfd23
);

print "only the numbers at the beginning before some alpha text:";
for ( @x ) {
print $1 if /^(\d+)(?=\D)/;
}

print "only the numbers within the alpha text:";
for ( @x ) {
print $1 if /(?<=\D)(\d+)(?=\D)/;
}

print "only the numbers at the end after some alpha text:";
for ( @x ) {
print $1 if /(?<=\D)(\d+)$/;
}
'
only the numbers at the beginning before some alpha text:
324
34
15
44
only the numbers within the alpha text:
21
8
only the numbers at the end after some alpha text:
56
23



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: show only numbers before, within or after the text
    ... sisyphus wrote: ... How can I obtain (Perl - Windows/commandline/singleline) the ... only the numbers at the beginning before some alpha text, ...
    (perl.beginners)
  • Re: show only numbers before, within or after the text
    ... How can I obtain (Perl - Windows/commandline/singleline) the ... only the numbers at the beginning before some alpha text, ... if{$x = undef} ...
    (perl.beginners)
  • Re: unicode
    ... I guess you could probably try to spell "Alpha" correctly. ... I am not an expert in Perl, but that might fix your problem:-D ... and I must turn on UTF-8 encoding of IO using the -C option to achieve ...
    (comp.lang.perl.misc)
  • Re: show only numbers before, within or after the text
    ... Luca Villa schreef: ... How can I obtain (Perl - Windows/commandline/singleline) the ... only the numbers at the beginning before some alpha text, ...
    (perl.beginners)
  • Re: asm-{alpha,h8300,um,v850,xtensa}/param.h: unbreak HZ for userspace
    ... without it currently perl 5.10 doesn't build on alpha: ... hmm it seems more userspace expects CONFIG_HZ, so it's not only a perl ... go fix the package. ... i'm guessing you're thinking packages are using CONFIG_HZ when in reality they ...
    (Linux-Kernel)