How to replaces a value in a file after a particular string using perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replaces a value in a file after a particular string using perl?
# 1  
Old 01-30-2013
RedHat How to replaces a value in a file after a particular string using perl?

I need to replace the value of notifications_enabled to 0 if the value already set to 1 and vice versa(not the other values and spaces remain same after the value changed). I tried the below program for that. Can any one help me out.

file:test.cfg
Code:
define host {
                   hostgroups                    linux-servers
                   max_check_attempts            5
                   check_interval                5
                   retry_interval                1
                   notification_interval        60
                   notifications_enabled         1
                 }

script:
Code:
#!/usr/bin/perl
use strict;
use vars qw( @value $status $line);

@value = `grep -i notifications_enabled /root/test.cfg`;
 open(FILE,"/root/test.cfg") || die unable to open the config_file;
 while ($line = <FILE>)
 {
   chomp $line;
   @value = `grep -i notifications_enabled /root/test.cfg`;
   $status = (split(/\s{2,}/,$value[0]))[1];
 }
print "$status\n";
close(FILE);

open(FILE,">>/root/test.cfg") || die unable to open the config_file;
while ( $line = <FILE>)
{
  print "$status";
if ( $status == 1) {
         $status = 0;
 }
}
close(FILE);

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by Scott; 01-30-2013 at 09:01 AM.. Reason: vbe: code tags - format... Using code tags preserve the format you know...; Scott: Removed poll
# 2  
Old 01-30-2013
Code:
$ cat tmp/tmp.dat
define host {
    hostgroups                    linux-servers
    max_check_attempts            5
    check_interval                5
    retry_interval                1
    notification_interval        60
    notifications_enabled         0
}


$ perl -pi -e's/(notifications_enabled\s*)(\d+)/$sub = "$1" . ("$2"== "0" ? "1":"0")/e; ' tmp/tmp.dat


$ cat tmp/tmp.dat
define host {
    hostgroups                    linux-servers
    max_check_attempts            5
    check_interval                5
    retry_interval                1
    notification_interval        60
    notifications_enabled         1
}


Last edited by Skrynesaver; 01-30-2013 at 10:49 AM..
# 3  
Old 01-31-2013
RedHat

Thanks Smilie ...it's working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

3. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

4. Shell Programming and Scripting

Searching a string in a file using perl

Hi I would like to read a file using perl and search for a string (last entry). Then read that into an array and do further grep File content for ex: comp=a,value=30,runtime=12,type=lic comp=d,value=15,runtime=2,type=lic comp=a,value=90,runtime=43,type=lic... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies

5. Shell Programming and Scripting

Perl Look for string in file

Hi Guys In perl how can i look for a string in side a file and return something if it exists a little bit of background i have made a wrapepr for a program but in order for the program to work i need to modify a file first, i want to stick something in the wrapper that will tell if the sting... (1 Reply)
Discussion started by: ab52
1 Replies

6. Shell Programming and Scripting

How would I do 2 search & replaces in 1 Perl statement?

Hi All, The below code successfully tails the logfile.log file and colors every word "ERROR" in RED, thanks to the Perl statement below. However, would anyone know how to append 1 additional search/replace to the Perl statement below, to color the word "SUCCESS" in GREEN (using ANSI \e ... (3 Replies)
Discussion started by: chatguy
3 Replies

7. UNIX for Dummies Questions & Answers

Get First String in a file-Perl

Is it possible, Using Perl script, to exit the program, when I get the first occurrence of a string. For eg, if I get a string "break" in a file (during parsing), i want to exit ! I tried using "filehandle". giving the string and file-name as commandline arguments. (3 Replies)
Discussion started by: guptesanket
3 Replies

8. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

9. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

10. Shell Programming and Scripting

Perl: searching for a string in a file...

Hi All, I need to search for a string in a file that I've opened and base a decision on the result. The logic is this: "if the word 'Shared' appears on the first line then do this on the whole file else do this on the whole file " The code I currently have isn't working:... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question