Perl Script remove digits and convert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script remove digits and convert
# 1  
Old 08-18-2013
Perl Script remove digits and convert

Hello Guy's
Quick question which im sure many can answer in seconds. Basically I have a perl script which is running commands to an element and then taking some of the
the output and printing it to the screen. One of the outputs is a variable Hex Number. What I would like to do is strip all the digits from this number apart from the
last 2 and then convert the last two digits to decimals.

I have taken some of the Script and printed below so you get an idea of what im doing.

Code:
my $cid;
        if ($ip) {
                for (`./ssh_run_cmd.exp $target $password "show **** ***** ****** $ex1"`) {
                        if (/0x\S+\s+(\S+)/) {
                                $cid = $1;   # THIS IS WHERE THE HEX NUMBER IS PULLED (EXAMPLE  0x11d14)
                                last;
                        }
                }
        }
        if ($ip) {
                for (`./ssh_run_cmd.exp $target $password "show ****** ******* ****** **** peer-address $ip"`) {
                       if (/*********\s+:\s+(\S+)/) {
                           print "Found $EXP1 in ", ( $activeoridle ? 'Idle' : 'Active' ), " mode  registered on ", $targets{$target}, " and using $1\n";
                                exit 0;

As you can see in the first section $cid is pulled from the output and then essentially what I would like to do is edit the $cid only to take the last two digits
and then convert the the last two digits into decimal to be displayed in the print output at the end.

Thanks for everyone's help in advance.
# 2  
Old 08-18-2013
Quote:
Originally Posted by mutley2202
...One of the outputs is a variable Hex Number. What I would like to do is strip all the digits from this number apart from the
last 2 and then convert the last two digits to decimals.
...
Code:
...
                        if (/0x\S+\s+(\S+)/) {
                                $cid = $1;   # THIS IS WHERE THE HEX NUMBER IS PULLED (EXAMPLE  0x11d14)
                                last;
                        }
...

...
So if I understood correctly, you want to:
(1) extract "14" from your example "0x11d14"
(2) consider 14 to be a hexadecimal number
(3) convert 14(hex) to dec, which is 20(dec), and
(4) assign 20 to $cid

Is that what you want to do?

If your example hexadecimal number was "0xabcde", would you want the conversion to be:

0xabcde => de => de(hex) = 222(dec) => 222 ?


Code:
$ 
$ perl -e 'BEGIN {$cid = "0x11d14"}  
           print "Original number                  : ", $cid, "\n";
           $cid =~ s/^.*(..)$/$1/;
           print "Last two characters              : ", $cid, "\n";
           print "Dec value of last two characters : ", hex($cid), "\n"
          '
Original number                  : 0x11d14
Last two characters              : 14
Dec value of last two characters : 20
$ 
$ 
$ perl -e 'BEGIN {$cid = "0xabcde"}
           print "Original number                  : ", $cid, "\n";
           $cid =~ s/^.*(..)$/$1/;
           print "Last two characters              : ", $cid, "\n";
           print "Dec value of last two characters : ", hex($cid), "\n"
          '
Original number                  : 0xabcde
Last two characters              : de
Dec value of last two characters : 222
$ 
$


Last edited by durden_tyler; 08-18-2013 at 01:59 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Can someone convert this python script to perl

There is a python script that I would like converted to a perl script. If someone has the time to convert the script I would appreciate it. You can find the script below: reboot-mb8600/reboot-mb8600.py at master . j4m3z0r/reboot-mb8600 . GitHub #!/usr/bin/python ''' A hacky script to... (1 Reply)
Discussion started by: azdps
1 Replies

3. Programming

DB2 convert digits to binary format

Dear Team We use DB2 v10.5 and using DBArtisan tool Can someone please guide how to convert digits to binary numbers using db2 feature. Ex> for number 9 , binary should be 1001 ( 8+1) Any help appreciated. Thanks (2 Replies)
Discussion started by: Perlbaby
2 Replies

4. Shell Programming and Scripting

Remove all digits and rename a file

Hi, I have a file nexus-1234 in a directory. I want to generate a random number and replace the 1234 with it and rename the file. So nexus-1234 becomes nexus-2863 after running the script. Any help is appreciated. Thanks in advance. (2 Replies)
Discussion started by: scorpioraghu
2 Replies

5. Shell Programming and Scripting

Convert shell script to Perl

Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first... find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
Discussion started by: gvolpini
1 Replies

6. Shell Programming and Scripting

Help with convert awk script into perl

Input file (a list of input file name with *.txt extension): campus.com_icmp_ping_alive.txt data_local_cd_httpd.txt data_local_cd.txt new_local_cd_mysql.txt new_local_cd_nagios_content.txt Desired output file: data local_cd_httpd data local_cd new local_cd_mysql new ... (9 Replies)
Discussion started by: perl_beginner
9 Replies

7. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

8. Shell Programming and Scripting

Convert .sh script into perl

Good afternoon to you all I really need your help I have the following script developed in .sh and I need to convert it into perl. Can someone help me do it please? Here´s the script: ############################################## ############################################## ... (3 Replies)
Discussion started by: zarahel
3 Replies

9. Shell Programming and Scripting

How to convert a column to last five digits

Hi Suppose I have a input.csv file like this Fort, 2034440-3333 Honda, b293489289 Toyota, 23423000 How to convert the second column to the following format, there are only two cases: a.if the last five character is a hyphen plus four numeric digit, keep the the last five digits... (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Need to convert ksh script to Perl

Guys I am new to this forum, this may seem like a cheeky request. I have been asked by my manager to convert this ksh script to Perl. I do not have the foggiest about Perl and would appreciate any help on this. Basically this scipt automates a recovery process for EMC Legato Networker. It will... (1 Reply)
Discussion started by: rahimm1
1 Replies
Login or Register to Ask a Question