Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hey guys

I have an interesting one today, I am trying to read a delimited file and create an output file with the information in neat columns. Whats so interesting I hear you say...?

Well I only need to convert between the line starting with 99HEADER to 99TERMIN. The delimitation is a pipe ( | ) but I was told by a colleague that pipes cause perl to do weird things (funny if true), so I created a tiny Shell script to convert this into a .csv file. So here goes...
#! /usr/bin/perl -w #Read between lines 40 - 112 from *.in, cut the lines up using pipe de +limitation. use strict; my @files = <*.in>; for my $file (@files) { open(my $read, '<', $file) or die "Couldn't open: $!"; open(my $write, '>', "$file.read") or die "Couldn't open: $!"; my $lines; my($record, $code, $date, $calpercent$, $vehprn, $vehreg, $insrate +num, $insratetxt) print $write "Record\tCode\tDate\tCalculated Percentage\tVehicle P +RN\tVehicle Registration\tInsurer Rating Number\tInsurer Rating Text\ +n"; while(<$read>) { #@lines = split('', $_); if(/99HEADER/../99TERMIN/) { chomp; ($record, $code, $date, $calpercent$, $vehprn, $vehreg, $i +nsratenum, $insratetxt) = split(','); print $write "$record\t$code\t$date\t$calpercent$\t$vehprn +\t$vehreg\t$insratenum\t$insratetxt" } } }
And here is some sample data
0000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000 |0010001401N01 YY0101000000YYYYYY010100 01201309192013091920130919N12011 KY2 6AB 0250 + Y 02PN 999 20001010106 0200 04 00 0000 +0 0811 00 GB N 0300030201309195204120102200309052003090520130918 05R005000 K +Y2 6 3 001200M 1 1 N N N + KY2 6AB + 062013091901NNNNN 062013091902NNNNN 062013091920NNNNN 101001002013091919531118MMP20010101FM 19531118 010000000000000001N +05NNN NNN 00 N + 1120130919E04049E Y 15201309193820060101N10010101 99HEADER|001|001| 99INSSCH|AVP0| 99POLCOM|||||PIP735628020||||| 99INSFAC|F1_0|| 99INSFAC|F2_N|| 99INSFAC|F3_N|| 99INSFAC|F4_0|| 99INSFAC|F5_0|| 99INSFAC|F6_IM|| 99INSFAC|F8_0|| 99INSFAC|F9_B|| 99INSFAC|F10_0|| 99INSFAC|F11_3|| 99INSFAC|F12_0|| 99INSFAC|F13_Y|| 99INSFAC|F14_N|| 99INSFAC|F15_5|| 99INSFAC|F16_30/11/2011|20111130| 99INSFAC|F17_31|| 99INSFAC|F18_NO|| 99INSFAC|F19_246|| 99INSFAC|F20_B|| 99INSFAC|F21_0|| 99INSFAC|F22_5H|| 99INSFAC|F23_P|| 99INSFAC|F24_M|| 99INSFAC|F25_1598|| 99INSFAC|F26_13|| 99INSFAC|F27_9|| 99INSFAC|F28_17|| 99INSSCH|248| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRA01_1||||||"LNI10708"| 99INSSCH|391| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSGAL1||||||"W=P1|X=335|AB=0|BB=0|JF=569|HB=0|IB=0|AD=1|GC=I +1|KD=0|YE=335|BF=B|GF=0|KF=401,0,0,0|GSB=15|HSB=99.2468394|KDB=2377.3 +7934|LDB=A|UDB=0|ETB=155|HTB=51|URB=7"| 99INSSCH|116| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRZ01a||||||"00I10587"| 99INSFAC2|MSRZ01b||||||"335000 B"| 99INSSCH|216| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRZ01a||||||"00I10587"| 99INSFAC2|MSRZ01b||||||"335000 B"| 99HEADER|006|001 99INSSCH|091| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00156||||||I1P82240,CCCN0000,INNA0000,FAAA0570,YANZ1000| 99INSSCH|084| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00050||||||I1001569| 99INSSCH|052| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00124||||||XAAX0800,YPAX8400,ZAAZ0401,VAAA0000,WZZA0000| 99INSSCH|222| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00243||||||XAAX0800,YPAX8400,ZAAZ0401,VAAA0000,WZZA0000| 99TERMIN| ABC14480120070920 ABC24490120070920 AIL01620000000000 ALC06040120130902 ALC16050120130902 ALC26060120130902 ALC36070120130902 ALC46080120130902 ANS00720000000000 AVP01110119990101 AXT06750000000000 AXX00300120030430 AXX11680120030430 AXX35940120100509 AXX75190120080914 AXX95660120090819 B-Z43150000000000 B-Z53410000000000 B-Z64350000000000
Thanks Jim

In reply to Reading File and Seperating into columns by Jalcock501

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (17)
    As of 2015-07-31 14:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (278 votes), past polls