Hello, I'd appreciate any guidance on this -- I'm very new to PERL and can't crack this one. I have a pipe delimited file and the beginning of each line must start with | and end with || (no space in-between). Blank columns on a row should have a space inbetween the delimiters | |. I created the code below using regex to insert a space between the || delimiters anywhere a column might have blank data which is working ok.

Here's an example, the code below changes the file from:

|red||blue|yellow||black||

|white|yellow||teal||purple||

|red||black||yellow||teal||

To..(note spaces between delimiters for blank column data)

|red| |blue|yellow| |black||

|white|yellow| |teal| |purple||

|red| |black|yellow| |teal||

Here's the code

#!C:\Perl64\bin\perl -w use strict; # Iteration 1, open myfile.txt open(FILE, "</temp/PERL-Samples/myfile.txt") || die "File not found"; my @lines = <FILE>; close(FILE); #This section uses an array and scans each line and does a #sequential search and replace for the following strings: #1. Finds any instance of || and changes it to | | #2. Finds any instance of | || and changes it to | | | #3. Finds any instance of || | and changes it to | | | #4. Finds any instance of | | at the end of a line and #changes it to || my @newlines; foreach(@lines) { $_ =~ s/\|\|/\| \|/g; $_ =~ s/\| \|\|/\| \|/g; $_ =~ s/\|\| \|/\| \|/g; $_ =~ s/\| \|$/\|\|/g; push(@newlines,$_); } #Push changes to MyFileNew.dat open(FILE, ">/temp/PERL-Samples/MyFileNew.dat") || die "File not found +"; print FILE @newlines; close(FILE);

Another pre-existing condition this file has is there are several hundred lines where | and || exist on a line by themselves. Plus there are other lines not beginning with a | and ending with ||. For example:

|red| |blue|yellow| |black

||

|white|yellow| |teal| |purple||

|

red| |black|yellow| |teal

||

I'm not sure how to reliably and efficiently delete rows where || and | are the only characters in a line and also make sure each line is prefixed with | and suffixed with ||. Thanks in advance for reviewing this.


In reply to How do I delete lines matching a certain condition and make sure each line has the right prefix and suffix? by cjff150

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!
  • 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:
    & &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.