Hi all, I need to extract few strings from one file and paste it to another file.
My source file, i.e., my .h file, contains statements like: #define GMMREG_ATTACH_REQ_ID (GMMREG_PRIM_ID_BASE) /* @LOG GMMREG_ATTACH_REQ */ #define GMMREG_DETACH_REQ_ID (GMMREG_PRIM_ID_BASE | 0x0001) /* @LOG GMMREG_DETACH_REQ */ /* GMMREG primitives sent by the MM sub-layer to the MMI */ #define GMMREG_ATTACH_CNF_ID (GMMREG_PRIM_ID_BASE | 0x0010) /* @LOG GMMREG_ATTACH_CNF */ #define GMMREG_DETACH_CNF_ID (GMMREG_PRIM_ID_BASE | 0x0011) /* @LOG GMMREG_DETACH_CNF */ #define CCL2D_INIT_NCNF_ID (L2D_PRIM_ID_BASE+5) /* @LOG CCL2D_INIT_NCNF */ #define CCL2D_INIT_RESP_ID (L2D_PRIM_ID_BASE+6) /* @LOG CCL2D_INIT_RESP */ Basically it defines a set of message names(GMMREG_DETACH_CNF_ID) to its hex values((GMMREG_PRIM_ID_BASE | 0x0011) ). here GMMREG_PRIM_ID_BASE is initialized to some value.
From this file i need to extract three strings from each line. they are
1. GMMREG_ATTACH_REQ_ID 2. GMMREG_PRIM_ID_BASE 3. 0x0001 => this string can be of 2 digits or 4 digits or sometimes it can be a 2 digit decimal number too. the 3rd string is sometimes is not present. Once these are extracted, i can process it further to the requirement. I tried reading each line, splitting it using spaces, grouping them using pattern matching and then displaying. The code, which i wrote for this, is written at the end of this mail. But i am facing a issue like, the header file need not have spaces as in every line. A line can read like: #define GMMREG_DETACH_IND_ID (GMMREG_PRIM_ID_BASE | 0x0030) /* @LOG DETACH_IND */ It can also read like: #define CCL2D_INIT_NCNF_ID (L2D_PRIM_ID_BASE+5) /* @LOG CCL2D_INIT_NCNF */ So because of the same all the patterns are not getting identified. Since i am using spaces as delimiters here, i am facing problems. I tried using "|" or "+" or "(" or ")", but i am unable to get a proper working logic with these delimiters. Can anyone suggest any solution for this? Thanks and Regards, Dharshana P.S: My code reads: #!/usr/bin/perl unless (open(L3L4IN, "tf_l3l4_message_ids.h" )) { die("Cannot open input file \n"); } while($in = <L3L4IN>) { @array = split("/", $in); $length = @array; @final_result = split (/ +/, $array[0]); $res_len = @final_result; $entry1 = $entry2 = $entry3 = $entry4 = $entry5 = 0; for($j=0; $j<$res_len; $j++) { if($final_result[$j] =~ m/^ [A-Z]+ (?:_[A-Z]+)+ $/x) { $entry1 = $result[$j]; } elsif($final_result[$j] =~ m/^ \( [A-Z]+ (?:_[A-Z]+)+ $/x) { $str1_length = length($result[$j]); $a1 = substr($result[$j], 1, $str1_length); $entry2 = $a1; } elsif($final_result[$j] =~ m/0x(\S{4})/g) { $str2_length = length($result[$j]); $a2 = substr($result[$j], 0, $str2_length-1); $val = hex($a2); $entry3 = $val; } elsif($final_result[$j] =~ m/0x(\S{2})/g) { $str4_length = length($result[$j]); $a4 = substr($result[$j], 0, $str4_length-1); $val2 = hex($a4); $entry5 = $val2; } elsif($final_result[$j] =~ m/(\d{2})/g) { $str3_length = length($result[$j]); $a3 = substr($result[$j], 0, $str3_length-1); $val1 = int($a3); $entry4 = $val1; } } if(($entry1 ne "0")&&($entry2 ne "0")&&($entry3 ne "0")) { $entry_value1 = ($hash{$entry2}|$entry3); $entry_value = sprintf("%x", $entry_value1); print "$entry_value , $entry1\n"; } elsif(($entry1 ne "0")&&($entry2 ne "0")&&($entry4 ne "0")) { $entry_value1 = ($hash{$entry2}|$entry4); $entry_value = sprintf("%x", $entry_value1); print "$entry_value , $entry1\n"; } elsif(($entry1 ne "0")&&($entry2 ne "0")&&($entry5 ne "0")) { #print "$hash{$entry2} => $entry5\n"; $entry_value1 = ($hash{$entry2}|$entry5); $entry_value = sprintf("%x", $entry_value1); print "$entry_value , $entry1\n"; } }