Good day.
My text file content below. tmp.txt (a very big size file)
constant fixup private AlarmFileName = <A "C:\\TMP\\ALARM.LOG"> /* A Format */
constant fixup ConfigAlarms = <U1 0> /* U1 Format */
constant fixup ConfigEvents = <U2 0> /* U2 Format */
My parse code below.
The code can’t handle C:\\TMP\\ALARM.LOG (quoted string) here.
I don’t know how to replace the code “s+([a-zA-Z0-9])+>” to handle both the [a-zA-Z0-9] (0 above) string and the quated string (“C:\TMP\ALARM.LOG” above).
$source_file = "tmp.txt";
$dest_xml_file = "my.xml";
#Check existance of root directory
open(SOURCE_FILE, "$source_file") || die "Fail to open file $source_file";
open(DEST_XML_FILE, ">$dest_xml_file") || die "Coult not open output file $dest_xml_file";
$x = 0;
print DEST_XML_FILE "<!-- from tmp.txt-->\n";
while (<SOURCE_FILE>)
{
&ConstantParseAndPrint;
}
sub ConstantParseAndPrint
{
if ($x == 0)
{
if(/^\s*(constant)\s*(fixup|\/\*fixup\*\/|)\s*(private|)\s*(\w+)\s+=\s+<([a-zA-Z0-9]+)\s+([a-zA-Z0-9])+>\s*(\/\*\s*(.*?)\s*\*\/|)(\r|\n|\s)/)
{
$name1 = $1;
$name2 = $2;
$name3 = $3;
$name4 = $4;
$name5 = $5;
$name6 = $6;
$name7 = $7;
printf DEST_XML_FILE "\t\t$name1";
printf DEST_XML_FILE "\t\t$name2";
printf DEST_XML_FILE "\t\t$name3";
printf DEST_XML_FILE "\t\t$name4";
printf DEST_XML_FILE "\t\t$name5";
printf DEST_XML_FILE "\t\t$name6";
printf DEST_XML_FILE "\t\t$name7";
$x = 1;
}
}
}
Thank you for your input.
**HELLO ALL,
Thanks for so many great solutions. I am a newbew, i would like to do more study based on your post.
THANKS A LOT.**
Use the following parse code:
I have added handling of both the single and double quotes. I use back-reference for quotes matching. Also I have updated the character class for path. i.e. it now includes the colon(:), dot(.), and backslash() along with alpha-numeric characters.