473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

MAtch expression and read modify file

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
10 1899
Icecrack
174 Expert 100+
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
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
4,059 Expert 2GB
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
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
174 Expert 100+
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
410 Expert 256MB
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
410 Expert 256MB
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
410 Expert 256MB
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
174 Expert 100+
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
4,059 Expert 2GB
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

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

Similar topics

3
by: William Holroyd | last post by:
I've had experience with PHP building simple websites and have only scratched the surface of what it's capable of, so in a sense I'm a newbie. However the problem I have is that I want to be able...
0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
12
by: Johnny Williams | last post by:
I'm struggling to create a regular expression for use with VB .Net which matches a person's name in a string of words. For example in "physicist Albert Einstein was born in Germany and" I want...
12
by: cmk128 | last post by:
Hi PHP's regular expression look like doesn't support .*? syntax. So i cannot match the shortest match. For exmaple: $str="a1b a3b"; $str1=ereg_replace("a.*b", "peter", $str1); will produce...
5
by: Peng Yu | last post by:
Hi, The following code snippet is from /usr/bin/rpl. I would like the it to match a word, for example, "abc" in ":abc:". But the current one would not match "abc" in ":abc:". I tried to modify...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.