Changing file content in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing file content in perl
# 1  
Old 03-11-2014
Hammer & Screwdriver Changing file content in perl

Hi All,

I have a file content like this.

Code:
#<clear_category_list>

#<include file="SUITE:Provision-FLEXPONDER_FAC.inc">

#<include file="CAT:SYSTEM:SAS_U23.inc">
#<include file="CAT:PRIORITY:10.inc">
#<include file="CAT:FAC_TYPE:OC48.inc">
#<include file="CAT:FAC_TYPE:OC192.inc">
#<include file="CAT:FAC_TYPE:OCH107.inc">
#<include file="CAT:FAC_TYPE:OCH111.inc">
#<include file="CAT:FAC_TYPE:OCN.inc">
#<include file="CAT:FAC_TYPE:10GE.inc">
#<include file="CAT:CARD_TYPE:IFMA-BU.inc">
#<include file="CAT:BOARD_TYPE:cpp8548.inc">
#<include file="CAT:SCHEDULER:2.inc">

#-------------------------------------------------------------- 

#<testcase_begin testcase_id="SAS_U23_FAC10_Regression">
 testcase_title="SAS_U23 FAC10 Regression">

Where i can search only by using the pattern "SAS_U23_FAC10_Regression"
There are lots of content in this file.

What i want is to change the line

Code:
#<include file="CAT:PRIORITY:10.inc">

to

Code:
#<include file="CAT:PRIORITY:3.inc">

and i want to count of how many i have changed, I also want the count like if it is already changed or not,

I have tried the below code to find the pattern match, and i could not proceed further, can u all pls guide?

Code:
#!/bin/perl

open(fp, "<file_name");

while(<fp>)
{
    $line = $_;
    my $N = 1;  # Number of lines before matching line.
    my $PATTERN = "SAS_U23_FAC10_Regression"; # What you want to match.
    my @buffer;
    while (my $line = <fp>) {
        if (@buffer < $N) {
            push @buffer, $line;
            next;
        }
        if ($line =~ /$PATTERN/) {
            
            print $buffer[0];
        }
        shift @buffer;
        push @buffer, $line;
    }
}

# 2  
Old 03-11-2014
Could this help you ?
Code:
perl -nle 'BEGIN{$count=0;$cnt=0} if(/#\<include file="CAT:PRIORITY:3.inc"\>/) { ++$count}
if(/#<include file="CAT:PRIORITY:10.inc">/) {s/#<include file="CAT:PRIORITY:10.inc">/#<include file="CAT:PRIORITY:3.inc">/;
++$cnt; }
print $_;
END { printf "count changed now - $cnt\nCount Already Changed - $count\n";}' filename

# 3  
Old 03-12-2014
Wrench

Thanks for the help,

but i have a file content like this,

Code:
#==============================================================

#<clear_category_list>

#<include file="SUITE:Security-Security.inc">

#<include file="CAT:SYSTEM:U75_U23.inc">
#<include file="CAT:PRIORITY:1.inc">
#<include file="CAT:FAC_TYPE:DEFAULT.inc">
#<include file="CAT:CARD_TYPE:MPMA-SHP3.inc">
#<include file="CAT:BOARD_TYPE:cpp8548.inc">
#<include file="CAT:SCHEDULER:3.inc">

#-------------------------------------------------------------- 

#<testcase_begin testcase_id="U75_U23_User_Security_Test">
 testcase_title="U75_U23 User Security Test">

#<testcase_requirements requirement_id="REGRESSION_REQ_ID">
#<testcase_script script="run_regression_usersec  -findboard ${BOARD_TYPE} -next -stop -var SYSTEM ${CATEGORY_SYSTEM} -schedule +${SCHEDULER}h ">

#<testcase_objectives_begin>
test the FEATUREGROUP feature for FEATURE for CARDTYPE
#<testcase_objectives_end>

#<testcase_setup_begin>
#<testcase_setup_end>

#<testcase_procedure_begin>
#<testcase_procedure_end>

#<testcase_expected_results_begin>
#<testcase_expected_results_end>

#<testcase_end>


#==============================================================

#<clear_category_list>

#<include file="SUITE:SAS_HK_MR537_Reg.inc">

#<include file="CAT:SYSTEM:SAS_U23.inc">
#<include file="CAT:PRIORITY:1.inc">
#<include file="CAT:FAC_TYPE:DEFAULT.inc">
#<include file="CAT:CARD_TYPE:DEFAULT.inc">
#<include file="CAT:BOARD_TYPE:cpp8548.inc">
#<include file="CAT:SCHEDULER:1.inc">

#-------------------------------------------------------------- 

#<testcase_begin testcase_id="SAS_U23_HK_MR537_Regression_Test">
 testcase_title="SAS_U23 HK MR537 Regression">

#<testcase_requirements requirement_id="REGRESSION_REQ_ID">
#<testcase_script script="run_regression_hk_r71  -findboard ${BOARD_TYPE} -next  -stop -var SYSTEM ${CATEGORY_SYSTEM} -schedule +${SCHEDULER}h ">

#<testcase_objectives_begin>
test the FEATUREGROUP feature for FEATURE for FACTYPE
#<testcase_objectives_end>

#<testcase_setup_begin>
#<testcase_setup_end>

#<testcase_procedure_begin>
#<testcase_procedure_end>

#<testcase_expected_results_begin>
#<testcase_expected_results_end>

#<testcase_end>


wheer i should search for the testcase based on the various "testcase_begin testcase_id" present, and then move up to search the keyword PRIORITY:10 or any number and then change it as PRIORITY:3

And then find the count.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Perl SCript to read file content (if else statemenet)

Hi All, I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this. If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero) else if (content =0), it will update the content to... (11 Replies)
Discussion started by: hploh
11 Replies

3. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

4. Shell Programming and Scripting

perl extract content of file

I'm using Mail::Internet module, which will basically filter through email content and extract the body of the message my perl script to extract the body of the email #!/usr/bin/perl -w use Mail::Internet; @lines = <STDIN>; $mi_obj = new Mail::Internet(); ... (2 Replies)
Discussion started by: amlife
2 Replies

5. Shell Programming and Scripting

Need Help!!! For changing the content of the line

I need to change the content of the line after I grep it. The line is : Date SNWD.I-1 (in) WTEQ.I-1 (in) PREC.I-1 (in) TOBS.I-1 (degC) TMAX.D-1 (degC) TMIN.D-1 (degC) TAVG.D-1 (degC) I want to change the second column (SNWD.I-1(in)) to SNWD.I-1(m), how could I do that? I use csh... (2 Replies)
Discussion started by: handsonzhao
2 Replies

6. Shell Programming and Scripting

changing files content with sed or awk

Hi, Example File: (jumped, bumped, ) how to jumped, FROM tree; EXIT I have some hundreads of files like this with the different words and I want to remove the comma before the bracket and also I have to remove the comma before FROM word. I am trying to use this command : awk '... (5 Replies)
Discussion started by: rajshashi
5 Replies

7. UNIX for Dummies Questions & Answers

Changing file content based on file header

Hi, I have several text files each containing some data as shown below: File1.txt >DataHeader Data... Data... File2.txt >DataHeader Data... Data... etc. What I want is to change the 'DataHeader' based on the file name. So the output should look like: File1.txt >File1 ... (1 Reply)
Discussion started by: Fahmida
1 Replies

8. Shell Programming and Scripting

Perl search and replace file content.

I am not sure if this is doable. I am trying to open and print the content of the file by replacing all instances fo perl to PERL . This is my code but it is giving me the number count instead of the actual lines with changes. open (PERLHISTORY, 'sample.txt') or die "The file sample.txt could... (3 Replies)
Discussion started by: jxh461
3 Replies

9. Shell Programming and Scripting

file content in an array PERL

Hello everybody, I'm new in this forum. I searched a long time for a solution for my problem but I didn't find the right thing. I have to read from a file (content is "abngjm" without any other signs) and have to write this content in an array. But every sign has to be called by its own... (5 Replies)
Discussion started by: e_prof
5 Replies

10. UNIX for Dummies Questions & Answers

Need help with changing bash to perl

Hi guys, I am converting a bash script to perl. I need lots of help and pointers on how to make the script work. Any help would be greatly appreciated. Here is what I have: #!/usr/bin/perl #Decrypt Files $dir = "/usr/bin/gpg; opendir(PGP_DIR, $dir) || die "can't opendir $dir: $!";... (3 Replies)
Discussion started by: freak
3 Replies
Login or Register to Ask a Question