By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

MAtch expression and read modify file

P: 3
I want to read a file from a point where a regular expression matches and keep on reading till i hit another regular expression; while reading this part of the file ( binary strings of known size; search the appropriate position, parsed through a fil, make a replacement);
Oct 2 '08 #1
Share this Question
Share on Google+
10 Replies


Icecrack
Expert 100+
P: 174
Look theres that Line that im looking for look right there thats the line right there
you cant see it ???

what have you attempted so far?
Oct 2 '08 #2

P: 3
Adding more to what I wrote earlier:
there are two input file: used in step 1) & 3) and second one at step 2

parsed the input file which basically gives me three variables;
out of three varibles two are regular expression and one integer,
I match the first regular expression then after two line i have hard coded expression then i match my second regular expression from this point onwards there are strings of binaries in multiple line and i need to count the bits to the value equal to third variable (integer) and replace with known character;
then i pick another line from input file and repeatt the same process;

In short here is what i have done (step 1 &3) and where i am stuck (step 2 ):
1)Open and read first line of inputfile1 which gives a string and eventually three variables
2) two are regular expression one integer; use them to rplace the binary with know character; i am stuck as to how i read and replace the binary with know character
3) repeat 2 with next line from input file

I have done 1) & 3).............missing the heart of the problem step 2)
Oct 2 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
I parsed the input file which basically gives me three variables;
out of three varibles two are regular expression and one integer,
I match the first regular expression then after two line i have hard coded expression then i match my second regular expression from this point onwards there are strings of binaries in multiple line and i need to count the bits to the value equal to third variable (integer) and replace with known character;
then i pick another line from input file and repeatt the same process;
in short here is what i have done and where i am stuck:
1)Open and read first line of inputfile1 which gives a string and eventually three variables
2) two are regilar expression one integer; use them to rplace the binary witj know character
3) repeat 2 with next line from input file

I have done 1) & 3).............missing the heart of the problem step 2)
Post the code you have written so far.
Oct 2 '08 #4

P: 3
I am completely new to Perl so plaese provide me the tips for efficient codind as well.........here is sample of the code has been written so far
Expand|Select|Wrap|Line Numbers
  1. open(INP2, "<$patFile") || die "Unable to open (-i) $patFile for input";
  2. open(INP1, "<$msffFile") || die "Unable to open (-i) $msffFile for input";
  3. #open (WR, ">$OutFile") || die "cannot open $OutFile for writing \n";
  4.  
  5. $scan_op = "0";
  6. $read_mss_operation = 0;
  7. @arr_msff = "";
  8. @arr_pat = "";
  9.  
  10.   while (<INP1>) {
  11.  
  12.  push(@arr_msff, $_);
  13. #Format is:     
  14. #1    ch_0    47          
  15.   }#    
  16.  
  17. #Read and store file2
  18. while (<INP2>) {
  19.     #file  format is:
  20. # ..........
  21. # ..........other lines in the file
  22. #              SCAN = 1 ==> EXP as number (scan_op)
  23. #    APPLY "grp0_unload" 0 =
  24. #        CHAIN "ch_0" = "0101010100101010... \
  25. #                               010100101000010100...\
  26. #                               .....
  27. #                              ......
  28. #        CHAIN "ch_1" = ""0101010100101010... \
  29. #                               010100101000010100...\
  30. #        ..        ....
  31. #    ..        .... 
  32. #    END;
  33.     push(@arr_pat, $_);
  34. }
  35. &get_msff_line();
  36. #//End of main function
  37. ################################### Sub routines###########################
  38.  
  39. sub get_msff_line {
  40.  
  41.     while ($read_mss_operation <= $#arr_msff){
  42.         my $one = get_msff_info($read_mss_operation)  ;
  43.         chomp($one);
  44.         my ($scan_op,$ch_num,$mask_bit) = split (/ /,$one);
  45.     print "Scan Operation = $scan_op  Chain number = $ch_num  Masking Flop = $mask_flop" ;
  46.     print "\n";
  47.  
  48.  
  49. # Logic for Finding and replacing the bit in the inputfile2 for $scan_op for $mask_bit in $ch_num
  50.  
  51.        foreach ($i=0; $i <$#arr_pat; $i++){
  52.                    chomp $arr_pat[$i];
  53.         if ($arr_pat[$i]  =~ m/SCAN = $scan_op/){
  54.  
  55.         print "$arr_pat[$i]\n";
  56.         }
  57.  
  58.                }
  59.         $read_mss_operation++ ;
  60.     }
  61.  
  62. sub get_msff_info() {
  63.  $get_line = $_[0];
  64.     return $arr_msff[$get_line] ;
  65. }
  66.  
Oct 2 '08 #5

Icecrack
Expert 100+
P: 174
I am completely new to Perl so plaese provide me the tips for efficient codind as well.........here is sample of the code has been written so far

well

first off if your going to post code please use code tags it helps the experts read the code a lot easier if you want this solved then use code tags, i have missed a few errors in the script because of no code tags, so please use code tags.

second:

this could be a problem

Expand|Select|Wrap|Line Numbers
  1. while ($read_mss_operation <= $#arr_msff){
try

Expand|Select|Wrap|Line Numbers
  1. while ($read_mss_operation <= $arr_msff){
i have to leave for about 1 hr so ill continue to look at the rest of the script.


Thank You,
Oct 2 '08 #6

nithinpes
Expert 100+
P: 410
well

first off if your going to post code please use code tags it helps the experts read the code a lot easier if you want this solved then use code tags, i have missed a few errors in the script because of no code tags, so please use code tags.

second:

this could be a problem

Expand|Select|Wrap|Line Numbers
  1. while ($read_mss_operation <= $#arr_msff){
try

Expand|Select|Wrap|Line Numbers
  1. while ($read_mss_operation <= $arr_msff){
i have to leave for about 1 hr so ill continue to look at the rest of the script.


Thank You,
Icecrack,
arr_msff is an array variable (@arr_msff). $#arr_msff is used to get the last index of the array. That is not the problem.
Oct 3 '08 #7

nithinpes
Expert 100+
P: 410
In short here is what i have done (step 1 &3) and where i am stuck (step 2 ):
1)Open and read first line of inputfile1 which gives a string and eventually three variables
2) two are regular expression one integer; use them to rplace the binary with know character; i am stuck as to how i read and replace the binary with know character
3) repeat 2 with next line from input file

I have done 1) & 3).............missing the heart of the problem step 2)
To search for a pattern and replace it with any other character, you may use substitution operator(s///).
Expand|Select|Wrap|Line Numbers
  1. s/$search_pattern/replacement/ ; 
  2.  
Oct 3 '08 #8

nithinpes
Expert 100+
P: 410
Also, note the following:
1. If the words in your input file are separated with multiple spaces, replace
Expand|Select|Wrap|Line Numbers
  1. my ($scan_op,$ch_num,$mask_bit) = split (/ /,$one);
  2.  
with:
Expand|Select|Wrap|Line Numbers
  1. my ($scan_op,$ch_num,$mask_bit) = split (/\s+/,$one);
  2.  
2. There is no need of using a while loop to read from the file and push the lines to an array. You can directly use:
Expand|Select|Wrap|Line Numbers
  1. @arr_msff = <INP1>; 
  2. @arr_pat = <INP2>; 
  3.  
Oct 3 '08 #9

Icecrack
Expert 100+
P: 174
Icecrack,
arr_msff is an array variable (@arr_msff). $#arr_msff is used to get the last index of the array. That is not the problem.

thanks for the update never knew Perl did that with the array.
Learn Something new every day.
Oct 3 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
I get you are reading one file and that you need to use the data in the file to replace something in the other file, but what is it you are replacing?
Oct 3 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.