Operating System - HP-UX
1846983 Members
3478 Online
110257 Solutions
New Discussion

Remving the 1st letter of word in perl

 
paabhi
Frequent Advisor

Remving the 1st letter of word in perl

Hi All,

In one of the sections in my perl script, I need to remove the 1st character of a word and store in an array.The details are ,

The original script was ,

if ( ! defined $inq{$node}{"dmx"} ) {
print "INFO: Collecting inq disk information for DMX array on $node.\n";
$command="/opt/admin/bin/inq -sym_wwn -nodots |grep dsk";
my @clarinq;
($rcode, @clarinq) = execute_command($node, $command, "root");
#print Dumper @clarinq;
my ($ldev, $disk, $diskdevice, $line);
foreach $line (@clarinq) {
($diskdevice, $junk, $ldev, $junk) = split (/\s+/, $line);
($junk, $junk, $junk, $disk) = split (/\//, $diskdevice);
#print "disk is $disk, ldev is $ldev\n";
$inq{$node}{"dmx"}{$disk}=$ldev;
}
}

The o/p of /opt/admin/bin/inq -sym_wwn -nodots |grep dsk is in the format,

# /opt/admin/bin/inq -sym_wwn -nodots |grep dsk
/dev/rdsk/c25t0d0 000290103691 1C4D 60060480000290103691533031433444

The 3rd column is storing to "ldev".

In the new servers, the above inq command is not workin.We have the new command and the o/p of that command is in the format is,


$ pbrun /opt/admin/bin/inq.HPUXIA64 -sym_wwn -nodots|grep disk|grep -v HP |more
/dev/rdisk/disk8 000290103691 0237A 60060480000290103691533032333741

The 3rd column is of 5 digit and I want to remvoe the 1st character so that it will be in the same format as that of old servers.

I have tried this way.....
if ( ! defined $inq{$node}{"dmx"} ) {
print "INFO: Collecting inq disk information for DMX array on $node.\n";
$command="/opt/admin/bin/inq.HPUXIA64 -sym_wwn -nodots|grep disk|grep -v HP";
my @clarinq;
($rcode, @clarinq) = execute_command($node, $command, "root");
#print Dumper @clarinq;
my ($ldev, $disk, $ldev1, $diskdevice, $line);
foreach $line (@clarinq) {
($diskdevice, $junk, $ldev1, $junk) = split (/\s+/, $line);
($junk, $junk, $junk, $disk) = split (/\//, $diskdevice);
($junk, $ldev, $ldev, $ldev, $ldev) =split (/c/, $ldev1);=> This is not taking
#print "disk is $disk, ldev is $ldev\n";
$inq{$node}{"dmx"}{$disk}=$ldev;
}
}

Please could any one help me .

Ahilash
3 REPLIES 3
Hein van den Heuvel
Honored Contributor

Re: Remving the 1st letter of word in perl


Ahilash,

Thanks for a well presented problem.
The new device names are for the new 'agile', access path independent name.

If there is a 5th digit, it might be needed at some point in time... you realize that right?

anyway, i would suggest that instead of the repeat split you look for patterns instead.

You want
$1) the part after the last part in the first word
$2) the last 4 hex-characters in the 3rd word. An optional leading char

Using your 2 examples i van get that with:

$ perl -ne 'print qq($1 $2\n) if /^.*?(\/\w+)\s+\w+\s+.?([0-9A-F]{4})\s/' x.x
/c25t0d0 1C4D
/disk8 237A
^.*?(\/\w+)\s+ = remember last part of first word
\w+\s+ = second word
.?([0-9A-F]{4})\s = remember 4 chars in range, followed by whitespace, with optional '?' anything char.

In your code that can be used as:

if (/^.*?(\/\w+)\s+\w+\s+.?([0-9A-F]{4})\s/) {
$disk = $1;
$ldev = $2;
:


Good luck!
Hein
James R. Ferguson
Acclaimed Contributor

Re: Remving the 1st letter of word in perl

Hi:

> The 3rd column is of 5 digit and I want to remvoe the 1st character so that it will be in the same format as that of old servers.

If I understand correctly, you want lines like:

/dev/rdisk/disk8 000290103691 0237A 60060480000290103691533032333741

...changed to:

/dev/rdisk/disk8 000290103691 237A 60060480000290103691533032333741

For this, you could do:

# X="/dev/rdisk/disk8 000290103691 0237A 60060480000290103691533032333741"
# echo $X|perl -pe 's/(.+?\s)(.+?\s).(....)/$1$2$3/'

By the way, instead of doing:

# ($junk, $junk, $junk, $disk) = split (/\//, $diskdevice)

...use an array slice and do:

# $disk = (split /\//, $diskdevice)[3];

Regards!

...JRF...


Hein van den Heuvel
Honored Contributor

Re: Remving the 1st letter of word in perl

my typos:

part after the last part --> part after the last slash
i van --> I can

Your subject line indicated...

>> Remving the 1st letter of word in perl

Fortunately the body of the topic explained clearly what the real problem was that you were trying to solve.... retain the last 4 :-)

If one comes looking here for either those questions, then here is one of many ways how to do that.

remove first: $x =~ s/.//;
retain last 4: $y =~ s/.*(....)/$1/

In a working example:

$ perl -e '$x = $y = q(abcdefgh); $x =~ s/.//; $y =~ s/.*(....)/$1/; print qq($x $y\n)'

Other common ways to solve this and similar problems is to use the SUBSTR function, perhaps with the help of LENGTH to find where to start/end.

Hein