Sponsored Content
Top Forums Shell Programming and Scripting Perl Search and replace entire line Post 302198267 by insania on Thursday 22nd of May 2008 12:21:37 PM
Old 05-22-2008
Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file

Code:
sub changestate {
  my $base = ();
  my @base = ();

    open(BASE, $file) || die("Could not open file!");
    @base=<BASE>;
    close (BASE);

      foreach $base(@base)
        {
          if($base =~ /sslCACert=/){
             $base =~ s/sslCACert=/sslCACert=myserver.domain\/path/gi;
             print ("Hello, world!\n");
          }
  open (BASE, ">$file");
  print BASE @base;
  close (BASE);
    };
}

the problem is the line:

Code:
base =~ s/sslCACert=/sslCACert=myserver.domain\/path/gi;

The base sslCACert= I want to replace is followed by random garbage depending on which machine the file is on the only thing constant is sslCACert= . Can i use any wildcards? Also does a \ escape a / like in a shell script with perl. The Hello World is just there for testing.

Sean
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

2. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies

3. UNIX for Dummies Questions & Answers

Search for a string and copy the entire line

Hello All, I am after the script or the command which can scan the entire file for a string $PART_ID and when found to extract/copy the corresponding $PART_ID value (e.g THIRE_PTY_SOFTWARE for the 1st occurance of $PART_ID in the attached file) to a file. Appreciate your help. Thanks in... (3 Replies)
Discussion started by: forumthreads
3 Replies

4. UNIX for Dummies Questions & Answers

grep question: stop search from finding entire line

Sorry for the title, I really don't know how to word this question or what to even search for. I tried "grep one match", "grep 1 match", "stop grep" on both google and here and haven't found something that helps, so here I go: I have a file that's about 1.5 million lines long, every line looks... (3 Replies)
Discussion started by: rmoakler
3 Replies

5. UNIX for Advanced & Expert Users

command to replace the entire line from the key point

Hi everyone I am new to Unix. I got stuck up by small issue. I have text file something like this abc 'xyz' '5' lmn 'pqr' '7' i want to replace the abc 'xyz' '5' to abc 'xyz' '6' but i have a key as 'xyz' based on this key i want to do that. I am not aware of how to use sed... (7 Replies)
Discussion started by: Vijayaragavan
7 Replies

6. Shell Programming and Scripting

Replace entire line

I want to replace one line from my configuration file with the new settings. The file Name: /etc/httpd/conf/httpd.conf The following line should be replaced with the line mentioned below. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "\"%h\"... (3 Replies)
Discussion started by: shantanuo
3 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. UNIX for Advanced & Expert Users

Search and replace a line in perl

Hi All, i can replace a perticular value in sentence using perl. perl -pi -e 's/old/new/' sample.txt but i am not able to replace whole string by perl. file1 contains "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DATA=(SID= MWDBD22)))". i... (3 Replies)
Discussion started by: arindam guha
3 Replies

9. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

10. Shell Programming and Scripting

Help with sed to replace entire line

Hi, I need to replace an entire mailx line as follows using sed: sed -e 's/<line1>/<newline>/g' <filename> But I am getting comman garbled error since the new line has many special characters. I enclosed allspecial chars in \ but still no use. Can any one help me? Please use code... (2 Replies)
Discussion started by: vinodhin4
2 Replies
Sub::Override(3pm)					User Contributed Perl Documentation					Sub::Override(3pm)

NAME
Sub::Override - Perl extension for easily overriding subroutines SYNOPSIS
use Sub::Override; sub foo { 'original sub' }; print foo(); # prints 'original sub' my $override = Sub::Override->new( foo => sub { 'overridden sub' } ); print foo(); # prints 'overridden sub' $override->restore; print foo(); # prints 'original sub' DESCRIPTION
The Problem Sometimes subroutines need to be overridden. In fact, your author does this constantly for tests. Particularly when testing, using a Mock Object can be overkill when all you want to do is override one tiny, little function. Overriding a subroutine is often done with syntax similar to the following. { local *Some::sub = sub {'some behavior'}; # do something } # original subroutine behavior restored This has a few problems. { local *Get::some_feild = { 'some behavior' }; # do something } In the above example, not only have we probably misspelled the subroutine name, but even if their had been a subroutine with that name, we haven't overridden it. These two bugs can be subtle to detect. Further, if we're attempting to localize the effect by placing this code in a block, the entire construct is cumbersome. Hook::LexWrap also allows us to override sub behavior, but I can never remember the exact syntax. An easier way to replace subroutines Instead, "Sub::Override" allows the programmer to simply name the sub to replace and to supply a sub to replace it with. my $override = Sub::Override->new('Some::sub', sub {'new data'}); # which is equivalent to: my $override = Sub::Override->new; $override->replace('Some::sub', sub { 'new data' }); You can replace multiple subroutines, if needed: $override->replace('Some::sub1', sub { 'new data1' }); $override->replace('Some::sub2', sub { 'new data2' }); $override->replace('Some::sub3', sub { 'new data3' }); If replacing the subroutine succeeds, the object is returned. This allows the programmer to chain the calls, if this style of programming is preferred: $override->replace('Some::sub1', sub { 'new data1' }) ->replace('Some::sub2', sub { 'new data2' }) ->replace('Some::sub3', sub { 'new data3' }); A subroutine may be replaced as many times as desired. This is most useful when testing how code behaves with multiple conditions. $override->replace('Some::thing', sub { 0 }); is($object->foo, 'wibble', 'wibble is returned if Some::thing is false'); $override->replace('Some::thing', sub { 1 }); is($object->foo, 'puppies', 'puppies are returned if Some::thing is true'); Restoring subroutines If the object falls out of scope, the original subs are restored. However, if you need to restore a subroutine early, just use the restore method: my $override = Sub::Override->new('Some::sub', sub {'new data'}); # do stuff $override->restore; Which is somewhat equivalent to: { my $override = Sub::Override->new('Some::sub', sub {'new data'}); # do stuff } If you have override more than one subroutine with an override object, you will have to explicitly name the subroutine you wish to restore: $override->restore('This::sub'); Note "restore()" will always restore the original behavior of the subroutine no matter how many times you have overridden it. Which package is the subroutine in? Ordinarily, you want to fully qualify the subroutine by including the package name. However, failure to fully qualify the subroutine name will assume the current package. package Foo; use Sub::Override; sub foo { 23 }; my $override = Sub::Override->new( foo => sub { 42 } ); # assumes Foo::foo print foo(); # prints 42 $override->restore; print foo(); # prints 23 METHODS
new my $sub = Sub::Override->new; my $sub = Sub::Override->new($sub_name, $sub_ref); Creates a new "Sub::Override" instance. Optionally, you may override a subroutine while creating a new object. replace $sub->replace($sub_name, $sub_body); Temporarily replaces a subroutine with another subroutine. Returns the instance, so chaining the method is allowed: $sub->replace($sub_name, $sub_body) ->replace($another_sub, $another_body); This method will "croak" is the subroutine to be replaced does not exist. override my $sub = Sub::Override->new; $sub->override($sub_name, $sub_body); "override" is an alternate name for "replace". They are the same method. restore $sub->restore($sub_name); Restores the previous behavior of the subroutine. This will happen automatically if the "Sub::Override" object falls out of scope. EXPORT
None by default. BUGS
Probably. Tell me about 'em. SEE ALSO
o Hook::LexWrap -- can also override subs, but with different capabilities o Test::MockObject -- use this if you need to alter an entire class AUTHOR
Curtis "Ovid" Poe, "<ovid [at] cpan [dot] org>" Reverse the name to email me. COPYRIGHT AND LICENSE
Copyright (C) 2004-2005 by Curtis "Ovid" Poe This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.2 or, at your option, any later version of Perl 5 you may have available. perl v5.10.1 2010-04-01 Sub::Override(3pm)
All times are GMT -4. The time now is 09:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy