Ok guys,I'm just really new to perl programming. With the script below, I just wanna place a block of the input data to 2 different output files depending on the existence of a tag within each block. The input data format basically looks like this:
<DIVIDER>file.txt</DIVIDER>
<STANDARD>....
....
...
<DIVIDER>file.txt</DIVIDER>
>JN....
....
....
Basically, I just need to print only the data with the <STANDARD> tag to $outfile and those with the >JN to $fixfile. Within the process I'm also trying to strip the <DIVIDER> tag which doesn't seem to work. Please help me out, here's the snippet:
#!/bin/perl
$infile=ARGV[0];
$outfile=ARGV[1];
$fixfile="fix.txt"
open(INFILE,”$infile”) or die “can’t open $fixfile: $!” ;
open(OUTFILE,”>$outfile”) or die “can’t open $outfile: $!”;
open(FIXFILE,”>$fixfile”) or die “can’t open $fixfile: $!”;
$/= “<DIVIDER>*</DIVIDER>”;
$temp="";
while(<INFILE>) {
$*=1;
$temp=$_;
if ( $temp=~ /^<STANDARD>(.|\n)+/ ){
$temp=~s/<(DIVIDER)>.+<\/\1>//;
print OUTFILE $temp;
}
else {
print FIXFILE $temp;
}
}
close (INFILE);
close (FIXFILE);
close (OUTFILE);
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.