bks has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm executing a command and getting the output as mentioned below. I'm redirecting this output to a file and trying the get the first column of the output and put it in a different file.

However on execution of script through Cisco Network Compliance Manager/ HP Network Automation Tool. Opened the file where output is written and found that output is in garbled format(not in line by line format).

I need to pull the first column i.e. filenames and put it in a different file in line by line format. I thought of regex and wrote it to pull the filename but not sure how to fetch the matched values from a file. Regex: (.)((A-Za-zA-Za-z0-9_*))(.*)(\.PEM|m|r|y|t)

Please guide me.

Garbled Output will somewhat look this in the file

********************************

show crypto file[] 2FAWWW-2013.PEM 1675 PEM Yes KEY Apache-Portal_2012.pem 1675 PEM Yes KEY Onlineapply-creditcards-Portal-cer 2476 PEM Yes CERT Onlineapply-creditcards-Portal-key 1675 PEM Yes KEY Onlineapply_CC_prxy_1_2013.cer 2520 PEM Yes CERT Onlineapply_CC_prxy_2013.cer 2488 PEM Yes CERT

********************************

If written in line by line format then output will be as follows

********************************

2FAWWW-2013.PEM 1675 PEM Yes KEY

Apache-Portal_2012.pem 1675 PEM Yes KEY

Onlineapply-creditcards-Portal-cer 2476 PEM Yes CERT

Onlineapply-creditcards-Portal-key 1675 PEM Yes KEY

Onlineapply_CC_prxy_1_2013.cer 2520 PEM Yes CERT

Onlineapply_CC_prxy_2013.cer 2488 PEM Yes CERT

********************************

Want to use the regex and print the filenames/column1 in different file in line by line format as below

********************************

2FAWWW-2013.PEM

Apache-Portal_2012.pem

Onlineapply-creditcards-Portal-cer

Onlineapply-creditcards-Portal-key

Onlineapply_CC_prxy_1_2013.cer

Onlineapply_CC_prxy_2013.cer

********************************

Code used in NCM/HPNA Tool

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my($host,$port,$user,$pass) = ('localhost','$tc_proxy_telnet_port$','$ +tc_user_username$','$tc_user_password$'); my $device = '#$tc_device_id$'; my @output; my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, - +host => $host, -port => $port); $con->login(); $con->connect( $device ) or die "Failed to connect."; $con->cmd("terminal length 0"); print "show crypto files \n"; open my $fh, ">cryptofiles.log" or die "Cant open file died unexpected +ly"; @output = $con->cmd("show crypto files"); foreach (@output) { print $fh $_; } close $fh; $con->disconnect(); $con->logout(); undef $con; exit(0);

Thanks!

  • Comment on Fetch the regex matched values from file and write it to a file in line by line format.(Using HPNA to execute the script)
  • Download Code

Replies are listed 'Best First'.
Re: Fetch the regex matched values from file and write it to a file in line by line format.(Using HPNA to execute the script)
by keszler (Priest) on Aug 26, 2014 at 18:53 UTC
    It looks to me like each "line" ends with either KEY or CERT, and none of the filenames include space characters. Assuming that is true, and that you have/can get the "Garbled Output" into a single string, split could work:
    @filenames = split /\s.*?(?:KEY|CERT)/, $garbled_output;
    The regex is:
    /s whitespace .*? any character (.), unlimited length (*), as few as possible (? +) (?: non-capturing group KEY literal | OR CERT literal ) end group