Hi Monks,
I am trying to add a blank row above all lines starting with 10.
Example Source Data:
10record
record
record
10record
etc...
Required Output:
10record
record
record
10record
etc...
My following code is not picking up lines that don't contain 10:
#!usr/bin/perl -w
use strict;
use warnings;
open (DATA, "test.txt") || die "Unable to open file: $!";
open (OUT, ">testout.txt") || die "Cannot create out file: $!";
my $line = <DATA>;
my $type = substr ($line,0,2); # match first two characters in each li
+ne
NEWLINE: while (<DATA>) {
chomp; # remove terminating newline
if ($type eq "10") { # matches all record 10
print OUT "\n";
print OUT "$line\n";
last if ($type ne "10");
} # loop adds blank row above record 10
elsif ($type ne "10") { # matches all non record 10
print OUT "$line\n";
last if ($type eq "10");
}
next NEWLINE; # process next line
} # end while
close (DATA) || die "Unable to close file: $!";
close (OUT) || die "Unable to close file: $!";
Not sure what I have done wrong in exiting each loop.
Thanks in advance.
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.