Substitute Perl Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute Perl Script
# 1  
Old 03-18-2011
Substitute Perl Script

I am having trouble with a part of my substitute script I am using. I have it look through a file and find an exact match and then if it finds that match in the 1 file it should run the following 1 liner on 3 different files.
Code:
perl -pi -e 's/$CurrentName\s/$NewName/g' foo.cfg;

The issue that is happening is when I have names like:
Code:
Test
Test_Account_1
Test_Account_2

And I want to change the "Test" account to "Foo" I end up getting the following:
Code:
Foo
Foo_Account_1
Foo_Account_1

Instead of just replacing the exact match. Is there a way to turn that 1 line into an exact match only replace.

Thanks for any help on this.

Last edited by Franklin52; 03-21-2011 at 05:21 AM.. Reason: Please use code tags
# 2  
Old 03-18-2011
Try with \b,
Code:
perl -pi -e 's/\b$CurrentName\b/$NewName/g' foo.cfg;

# 3  
Old 03-18-2011
I tried it with \b and when I had it set to that it didn't change any of the names.
# 4  
Old 03-18-2011
Have you removed the \s ?
# 5  
Old 03-18-2011
Yeah, sorry the \s was my latest test I guess I should have removed that from my example.
# 6  
Old 03-18-2011
Hi, It's working at my end. Could you please attached your foo.cfg.
Code:
# cat foo.cfg
Test
Test_Account_1
Test_Account_2
# perl -nle 's/\bTest\b/Foo/g;print $_;' foo.cfg
Foo
Test_Account_1
Test_Account_2

# 7  
Old 03-18-2011
Below is a snippet from the file since it is quite large, there would be one of these for each entries.

Code:
# 'Test' host definition
define host{
        use                     generic-host
        host_name               Test
        alias                   Test
        address                 127.0.0.1
        check_command           check-host-alive
        max_check_attempts      3
        check_period            24x7
        notification_interval   120
        notification_period     24x7
        notification_options    d,u,r
        }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Substitute string using location (preferably perl).

I have a string like. ATATATATTATTATATTATATTATT I want to substitute the characters to "C" by using these locations 3 7 10 18 15 20 desired Output: ATCCCCCTTACCCCCCCCCCTTATT any clue will be great help. :wall: thanks in advance. (2 Replies)
Discussion started by: admax
2 Replies

2. Shell Programming and Scripting

How to substitute a varible in script having value including quotes?

Hi All, We need to run a Connect direct script on Unix server to send a file to Mainframe server and at mainframe end there need to run another job through Runtask with some parameters need to be passed from C:D (unix) to mainframe. My question is I have to pass parameters like DSN and FNAME as... (2 Replies)
Discussion started by: matrix001
2 Replies

3. Shell Programming and Scripting

substitute variable for values in perl

hi all, how do i assign values passed in from command line to and sql statement in perl ?? e.g i want to assign :name1 and :Name2 to be whatever is passed into the perl script command line my $sqlStr = "select * from test_table where column1 = upper(nvl(:name1, name1 )) and column2... (1 Reply)
Discussion started by: cesarNZ
1 Replies

4. Shell Programming and Scripting

Perl: substitute multiple lines

I'd like to do this using Perl command line (sorry no awk, sed, etc.) replace the 3 middle lines in a file by one line: aaa 123 bbb 234 ccc 34567 dd 4567 ee 567 to: aaa 123 AAA ee 567 I tried this but not working: perl -pi -e "s/aaa\ 123\nbbb\ 234\nccc\ 34567/AAA/" file (2 Replies)
Discussion started by: tintin78899
2 Replies

5. Shell Programming and Scripting

Shell script: substitute value in text file

Hi gurus, I need help with shell script. I have various INSERT queries which inserts data into database. I want to insert 3rd column data into newline for one particular table. I have very time long txt file everytime and it have various INSERT/UPDATE queries but i have to done with it only one... (8 Replies)
Discussion started by: mirfan
8 Replies

6. Shell Programming and Scripting

How to substitute the value with in double quotes in perl?

Hi, I have string like this: $str=' DNA OR ("rna AND binding AND protein")'; I just wanted to substitute AND with a blank. How can i do that? I want the output like this: $string= DNA OR ("rna binding protein") (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

How to substitute brackets in the beginning of string in perl?

Hi, I have a string like this user can specify different query sets that is why "or" is mentioned: $string="]("; or $string="](("; or $string="]((("; or $string="]((((("; (1 Reply)
Discussion started by: vanitham
1 Replies

8. AIX

pgrep substitute for porting a linux script to AIX 5.x

Hi, I'm trying to get this script to work on an AIX 5.3 box, I couldn't get pgrep for AIX, I also realize that ps works differently on the IBM boxes. Could anybody just give me the specifics of a work around for my problem, I'll adjust the whole script: #!/bin/bash # applabs.com #to do: #... (3 Replies)
Discussion started by: thebytegrill
3 Replies

9. UNIX for Dummies Questions & Answers

how to write perl substitute command in shell scripts

How to write perl substitute command in shell script without invoking a perl script file seperately. for ex: shell script to relace IT with CSC in a file using perl substitute command. (3 Replies)
Discussion started by: param_it
3 Replies

10. UNIX for Advanced & Expert Users

Problem with cat with PERL Substitute

system("cat FILENAME | perl -e 'while(<>) { print $_;}'"); system("cat FILENAME | perl -e 'while(<>) { $_ =~ s/XXX/YYY/g; print $_;}'"); First command works fine but second command gives the following error: syntax error at -e line 1, near "{ =~" syntax error at -e line 1, near ";}"... (2 Replies)
Discussion started by: jingi1234
2 Replies
Login or Register to Ask a Question