Perl - nested substitutions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - nested substitutions
# 1  
Old 07-26-2010
Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ...

sample data
Cisco Catalyst Operating System Software, Version 235.5(18)
Cisco Catalyst Operating System Software, Version 17.6(7)
Cisco Catalyst Operating System Software, Version 19.6(7)
Cisco Catalyst Operating System Software, Version 6.4(11)
Cisco Catalyst Operating System Software, Version 8.3(7)
Cisco Catalyst Operating System Software, Version 7.6(7)
Cisco Catalyst Operating System Software, Version 5.5(11)
Cisco Catalyst Operating System Software, Version 5.5(10)
SNMPv2-MIB::sysDescr.0 = STRING: Cisco IOS Software, 2800 Software (C2800NM-ADVIPSERVICESK9-M), Version 12.4(8), RELEASE SOFTWARE (fc1)

Korn I would do something like :

Code:
sed -e 's/.*ersion //i' -e 's/,.*//' $version



Perl

Code:
$version =~ s/.*ersion //i;
$version =~ s/,.*//;

sample output
17.6(7)
19.6(7)
6.4(11)
8.3(7)
7.6(7)
5.5(11)
5.5(10)
5.5(11)
7.6(7)
12.4(8)

I did google "perl + substitution" + "nest substitution" but didnt get any hits that explained nested substitution.

Thanks !!

---------- Post updated at 03:04 PM ---------- Previous update was at 02:19 PM ----------

Getting there ... just got to figure out how to git rid of dat blasted comma

$version =~ s/.*ersion ([\b\d{1,2}\.\d.*\(.*\)]+).*/$1/;


17.6(7)
19.6(7)
6.4(11)
8.3(7)
7.6(7)
5.5(11)
5.5(10)
5.5(11)
7.6(7)
12.4(8),

---------- Post updated at 03:21 PM ---------- Previous update was at 03:04 PM ----------

ok ... guess it came to me ..


$version =~ s/.*ersion ([\b\d{1,2}\.\d.*\(.*]+).*/$1\)/;

little tricky but it works.
# 2  
Old 07-26-2010
or

if ($input =~ /.*ersion (\d+\.?\d+?\(\d+\)).*?/) {
print $1;
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

2. Shell Programming and Scripting

Speeding up substitutions

Hi all, I have a lookup table from which I am looking up values (from col1) and replacing them by corresponding values (from col2) in another file. lookup file a,b c,d So just replace a by b, and replace c by d. mainfile a,fvvgeggsegg,dvs a,fgeggefddddddddddg... (7 Replies)
Discussion started by: senhia83
7 Replies

3. Shell Programming and Scripting

How can I write nested command substitutions?

Hello How can write the nested command substitutions? echo `expr substr $x 1 expr ${#x} - 1` the above code is not working! Thanks in advance Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

4. Shell Programming and Scripting

Two substitutions in one echo

PHOST1=temp i=1 I want to display the value of PHOST1 by making use of variable i inplace of 1 something like this echo "$PHOST$i" # -> This doesn't seem to work. Please provide me the correct syntax. I tried many different ways echo ${PHOST${i}} echo ${PHOST Nothing seems... (6 Replies)
Discussion started by: blazer789
6 Replies

5. Shell Programming and Scripting

Perl- Nested 'for' order change

Hello, I'm quite new to perl so my question is rather basic and I know there is probably a simple way around it but I can't seem to find it. I have a medium-length code and there is a part that works with a nested for loop: foreach my $j(@primpiddelta){ for (my $k=1;... (0 Replies)
Discussion started by: acsg
0 Replies

6. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

7. Shell Programming and Scripting

arrays and substitutions

I am working on a bash script and ran around this issue. here's the code : #!/bin/bash string="\"bin\" \"barn\" \"bin, barn /\"" array=($string) echo -e "\nMethod 1\narray is ---> ${array}" echo -e "array=($string)" array=("bin" "barn" "bin, barn /") echo -e "\nMethod 2\narray is... (4 Replies)
Discussion started by: titou_dude
4 Replies

8. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

9. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

10. Shell Programming and Scripting

Nested foreach in perl

I have two arrays @nextArray contains some files like \main\1\Xul.xml@@\main\galileo_integration_sjc\0 \main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\0 . . . \main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\0 @otherArray contains some files like ... (2 Replies)
Discussion started by: nmattam
2 Replies
Login or Register to Ask a Question