CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Regex pattern match (ish )

 



abs52
New User

Nov 22, 2016, 2:26 AM

Post #1 of 3 (7220 views)
Regex pattern match (ish ) Can't Post

Hi Guru's

I am in need of some help.

I have some perl that grabs the output of a command and then does some mataching on it

the output of the command looks like this ( the host-name changes for each host )


Code
HOSTNAME:               
blah
blah
blah
foo
foo
foo


and the perl that i had written was like this


Code
        if ($line =~ /^[a-z]/ && $line =~ /([\w-]+): $/) { 
$hostname = $1;
$host = $hosts->{$hostname};
# for time sync checking
$host->{date} = time();


Which was grabbing the first line ( the HOSTNAME) and then using it later

Now someone has change the output of the command to be like below and i am unsure how i can get the same results with the new layout


Code
hostname: blah 
hostname: blah
hostname: foo
hostname: foo


Any suggestions would be amazing

Thanks in advance


BillKSmith
Veteran

Nov 22, 2016, 6:01 AM

Post #2 of 3 (7215 views)
Re: [abs52] Regex pattern match (ish ) [In reply to] Can't Post

Use the function 'split'. (Refer to documentation in perldoc perlfunc)

Note: The code and data that you posted would not work as you describe.

  • Your regex requires the hostname to begin with a lower case letter. Your data is in upper case.


  • Your regex requires the hostname line to end with a colon and a single space. Your data has a long string of whitespace (including a newline) after the colon.


  • I probably could give a more complete answer if you post what you have tried and explain how if fails for the 'new 'data that you have posted.
    Good Luck,
    Bill


    abs52
    New User

    Nov 22, 2016, 7:04 AM

    Post #3 of 3 (7211 views)
    Re: [BillKSmith] Regex pattern match (ish ) [In reply to] Can't Post

    Hi Bill

    Sorry for the wrong info, in the fist example HOSTNAME: should have been in lower case.

    I had not done any changes to the code yet , but split looks good, i will give that a good

    Thanks for the information
    Adam

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives