Replacing strings in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing strings in perl script
# 1  
Old 07-28-2010
Power Replacing strings in perl script

HI all,

These are examples of the original value from a variable

$abc can be

FastEthernet1/0
GigabitEthernet3/1
Serial1/0

If $abc is FastEthernet*/* (where * can be any number), replace $abc value to fa*/* (same number as the original value). GigabitEthernet becomes ga*/* and Serial become se*/*


Appreciate your help a lot! Thanks!
# 2  
Old 07-28-2010
Is this what you are expecting?

Code:
$abc = 'ABC1/2';
$a = 'ABC';
$b = 'BALL';
$abc =~ s/$a/$b/g;
print $abc;

Ouput

Quote:
BALL1/2


---------- Post updated at 10:32 AM ---------- Previous update was at 10:20 AM ----------

Or Something like this, in case you want to replace more than one string at a shot

Code:
#!/usr/perl5/bin/perl

use strict;
use warnings;


my %replace = (
    FastEthernet => "fa",
    GigabitEthernet => "ga",
);

my $regex = join "|", keys %replace;
$regex = qr/$regex/;

my $s = "FastEthernet1/0 GigabitEthernet3/1 Serial1/0";

$s =~ s/($regex)/$replace{$1}/g;

print "$s\n";

Output

Quote:
fa1/0 ga3/1 Serial1/0
# 3  
Old 07-28-2010
Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. Shell Programming and Scripting

Perl script to find process and exclude strings from the output

Hi team, I'm a newbie of Perl Script and looking to create a simple perl script that will run in the Linux system: 1) to find process, such as ps -ef | grep process name 2) to exclude strings from the output if it found, for instance if i see abc from usr process, then will exclude it from... (1 Reply)
Discussion started by: hoffman2503
1 Replies

3. Shell Programming and Scripting

Replacing strings

Hello All, I have two files with delimited | file 1 : 1|2|3 11|12|13 22|23|24 and file 2 : 1|4|5|6 11|14|15|16 22|25|26 I want to replace the value '1' in file 2 with the values in file 1 '1|2|3' so the final output will look like 1|2|3|4|5|6 11|12|13|14|15|16 22|23|24|25|26 (3 Replies)
Discussion started by: ArunKumarM
3 Replies

4. Shell Programming and Scripting

Perl script to delimit size of strings

Hello, I have a huge file of over 2,00,00,00 strings in UTF8 format. I have managed to write a script in Perl which sorts them neatly as per their Unicode ranges. However I am now stuck with a script which will pipe out all strings between 3 and 20 letters/characters. I am not very good at... (2 Replies)
Discussion started by: gimley
2 Replies

5. Shell Programming and Scripting

script in perl for removing strings between a file

I have file that looks like: ATOM 2517 O VAL 160 8.337 12.679 -2.487 ATOM 2518 OXT VAL 160 7.646 12.461 -0.386 TER ATOM 2519 N VAL 161 -14.431 5.789 -25.371 ATOM 2520 H1 VAL 161 -15.336 5.698 -25.811 ATOM 2521 H2 VAL 161 -13.416 10.529 17.708 ATOM 2522 H3 VAL 161 -14.363 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

6. Shell Programming and Scripting

Replacing variable value in perl script

hi I have this line in my perl script if (($cookie =~ /^cookie(.*)\/(.*)/) && ($ !~ /^cookie(10)\/(.*)|/)) { $cookienumber = "$1.$2"; } now if the result is cookie1/0, my $cookienumber would be 1.0 but if the result is cookie1/0/0 ... (5 Replies)
Discussion started by: tententen
5 Replies

7. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

8. Shell Programming and Scripting

Replacing strings

I am trying to take the two line version of this: mv myFile.txt myFile.txt.bak sed 's/foo/bar/g' myFile.txt.bak > myFile.txt and make it into a shell script with three parameters. First two parameters are the string and string replacement and the third is file. So far this is what I have... (5 Replies)
Discussion started by: gordonheimer
5 Replies

9. Shell Programming and Scripting

awk script for replacing 2 strings

Hi I have written a script for automating a program. There is a string in 2 lines that needs altering from input. The 2 lines are: prepare_flexreceptor4.py -r rec_rigid.pdbqt -s TYR119_TRP312 -x rec_flex.pdbqt and prepare_flexdocking4.py -l ind.pdbqt -r rec_flex.pdbqt -s TYR119_TRP312... (3 Replies)
Discussion started by: gav2251
3 Replies

10. Shell Programming and Scripting

ideas for perl script - strings,conditionals..etc

I have a matrix , how do I compare all the elements of a column , lets say I want to check if the columns contain the alphabets "S","H","A","R","A","T". and not "X"s. Lets say matrix looks something like this .. SSSXSH HHXXHA AAXXAT RRRXRS AAXTAR TTTTTA I can hard code it where... (4 Replies)
Discussion started by: sharatz83
4 Replies
Login or Register to Ask a Question