×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading characters from within a line of text

Reading characters from within a line of text

Reading characters from within a line of text

(OP)
Hi,

  I have to extract a substring for a line of text. I have the line of text within $_ and need to extract a numerical value after a constant string in the line (hence I know where it will be).

  I have an idea of how to do this with a substr command, however is there a way of moving the current string matching position to the start of the substring I want?

e.g.

To extract the number 32 from the following

Preparing the Exercise Activity Log for CPU on path 32 file


I wouldn't be able to use the normal substr with numbers as the CPU bit could be something like MEMORY, or another keyword...

Any ideas greatly appreciated.
Cheers
Loon

RE: Reading characters from within a line of text

You could use pattern matching......

If the digits you are looking for will be the only digits in the string, you could

$string =~ /.*(\d+).*/;
$number = $1;

Which says match anything of any length, then some digits, then more anything of any length.  Then catch the contents of the paren's in $number.

If there might be more that one set of digits, that won't work.  So,......
If 'CPU' will change to MEMORY to DISK to some else, then morph your match pattern and then do the match.




keep the rudder amid ship and beware the odd typo

RE: Reading characters from within a line of text

(OP)
That's cool goBoating thanks!
The CPU and MEMORY lines can use the above, however, the others require something a bit more complicated as their 'numbers' (actually hardware paths on a unix server) will have . and / characters as they will be SCSI paths.

So can I use the same regexp but with an extra catch for . and / characters (until the next white-space)? I know very little about regexps... could someone show me how?

Many Thanks
Loon

RE: Reading characters from within a line of text

OK,  another shot

#!perl
$str = 'Preparing the Exercise Activity Log for SCSI on path /dev/dsk/c0t0d0s0 file';

# this would match CPU line but not current string.
if ($str =~ /.*(\d+).*/) { $number = $1; }

# failed to set number in previous line, should match this time
if ($str =~ /Preparing the Exercise Activity Log for SCSI on path (\/.*? )file/)
    { $number = $1;}

print "number is $number\n";



hope this helps...




keep the rudder amid ship and beware the odd typo

RE: Reading characters from within a line of text

(OP)
What about something along the following lines (I am still learning this regexp stuff so I don't think this will work):

$line = "-- Exercise Activity Log for SCSI Disk on path 10/0.6.0 --"
$line =~ tr/a-zA-z -//;

Would that transpose all alphabetical characters (and space and -) to nothing? Leaving the string:
-- Exercise Activity Log for SCSI Disk on path 10/0.6.0 --

Or, would this catch the right string?:

if (/.*[\d+]\/[\d+][\d+.]*.*/)
     ^   ^   ^  ^     ^  ^ ^
     ¦   ¦   ¦  ¦     ¦  ¦ ¦___ Any number of any char
     ¦   ¦   ¦  ¦     ¦  ¦_____ 0+ times
     ¦   ¦   ¦  ¦     ¦________ 1+ digits followed by .
     ¦   ¦   ¦  ¦______________ 1+ digits
     ¦   ¦   ¦_________________ followed by a /
     ¦   ¦_____________________ 1+ digits
     ¦_________________________ Any number of any char



I am presuming that the '/' needs the backslash to escape it and that the . inside the character class [] does not need escaping.

However, this does not seem to work. So can I just use a declaration like the following?:

$scsi_path = /.*[\d+]\/[\d+][\d+.]*.*/;

or should I be re-assigning $& or something? e.g.

$scsi_path = $&;


I'm a bit stuck here! Any help greatly appreciated!

Cheers,
Loon


RE: Reading characters from within a line of text

You're real close.....regex's take a little playing with.......If your string is fairly consistent and your example is truely representative, .......I don't think you need to get rid of any text, just catch the scsiPath pattern.
a space, some digits, a slash, more digits with decimals, a space

#!/usr/local/bin/perl
$str = '-- Exercise Activity Log for SCSI Disk on path 10/0.6.0 --';
# catch scsiPath pattern in $1
$str =~ / (\d+\/[\d+.]*) /g;
         ¦ ¦   ¦    ¦   ¦
         ¦ ¦   ¦    ¦   space
         ¦ ¦   ¦   [\d+.]* - repeating digits dot pattern
         ¦ ¦   \/ - escaped slash  
         ¦ \d+ - any number of digits
        space

$scsiPath = $1;
print "matched $scsiPath\n";

'hope this helps




keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close