1833874 Members
2192 Online
110063 Solutions
New Discussion

scripting questions

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

scripting questions

Hi all,

How do I write the below lines in PERL script.

line="/oracle/datafile/arclogs:"
HDR=`echo $line | cut -c 1-1`
HDR1=`echo $line | cut -f3 -d'/'`
HDR2=`echo $line | cut -f1 -d':'`

Thanks.
quest for perfections
9 REPLIES 9
Rajeev  Shukla
Honored Contributor

Re: scripting questions

Use this way,
#!/usr/bin/perl
$line = "/oracle/datafiles/arclogs";
@HDR = split(/\//, $line);
print("$HDR[1]\n");
print("$HDR[2]\n");
print("$HDR[3]\n");

This will have 3 variables with name $HDR[1], $HDR[2], $HDR[3], where @HDR is the array.

Cheers
Rajeev
Mark Grant
Honored Contributor
Solution

Re: scripting questions


or alternatively

$line="/oracle/datafile/arclogs:";
($HDR,$HDR1)=$line=~m#(.).+/(.+)/.+#;
($HDR2)=$line=~/(.+):/;
Never preceed any demonstration with anything more predictive than "watch this"
Rodney Hills
Honored Contributor

Re: scripting questions

A slight modification on Marks. "?" need to be added to quantative matches. (.+) will match the maximum number of times. "?" will match a minimum. For example if your path were /oracle/datafile/arclogs/bleem, then Marks pattern match will return "arclogs" for $HDR1 rather than the 3rd field "datafile".

Try this-

$line="/oracle/datafile/arclogs:";
($HDR2,$HDR,$HDR1)=$line=~m#^((.).+?/(.+?)/.+):#;

HTH

-- Rod Hills
There be dragons...
Ralph Grothe
Honored Contributor

Re: scripting questions

The first solution by Rajeev which uses split() is to be preferred since regex matching is more expensive (and error prone ;-).

Maybe overkill for your example, but you can also import the File::Basename module which is part of a core Perl installation.
This exports for you among others the functions dirname() and basename() which should be familiar to you from shell scripting
(though uncalled for exporting isn't considered polite as CPAN standards go ;-)

You can read the docs by issuing

perldoc File::Basename

and if your interested in the implementation use the -m switch

perldoc -m File::Basename
Madness, thy name is system administration
Mark Grant
Honored Contributor

Re: scripting questions

Actually Ralph, Rajeevs solution doesn't solve the problem. It looks at first sight that the questioner is asking for the directory names extracted out of the path but look carefully at the suppied script and you'll see that it isn't the case. HDR should end up as "/", HDR1 should have "datafile" and HDR2 has the whole path without the trailing ":". I don't know if this is what the poster actually wanted but it is what he asked for.
Never preceed any demonstration with anything more predictive than "watch this"
Ridzuan Zakaria
Frequent Advisor

Re: scripting questions

Hi all,

Mark is correct, HDR should return '/' ,HDR2 should return 'datafile' and HDR2 should return everything before ':'.

I have written the script in kornshell but it tooks very time to complete. Perhap perl will give better performance.

Thanks.
quest for perfections
Ridzuan Zakaria
Frequent Advisor

Re: scripting questions

Hi all,

Mark is correct, HDR should return '/' ,HDR1 should return 'datafile' and HDR2 should return everything before ':'.

I have written the script in kornshell but it tooks very time to complete. Perhap perl will give better performance.

Thanks.
quest for perfections
H.Merijn Brand (procura
Honored Contributor

Re: scripting questions

my $path = "/oracle/datafile/arclogs";
my @p = ($path =~ m{(/[^/]*)}g);

lt09:/home/merijn 101 > perl -le'$path=shift;@p=($path=~m{(/[^/]*)}g);print for@p' /oracle/datafile/arclogs:
/oracle
/datafile
/arclogs:
lt09:/home/merijn 102 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: scripting questions

Mark,

I admit I was too sloppy, not reading carefully enough the wanted result.

The only thing I wanted to stress is that whenever you can avoid Regexs, use functions like split(), or substr().
And if you can't or don't want to avoid them when you have a constant pattern, use the "o" modifier.

However, with the given result wanted you can avoid a regex, though admittedly by loss of elegance.

e.g.

$line = '/oracle/datafile/arclogs:';
($HDR, $HDR1, $HDR2) = (substr($line,0,1),(split(/\//,$line))[2],substr($line,0,
-1));
Madness, thy name is system administration