in reply to Re^2: copy line with character
in thread copy line with character

If you are still available, I do have another question? :) If I want to include the second line after the line containing M and wrap it all into one line on the output file what would I have to include?

sample of input file: M12345678 John Doe 11/20/15 M987654 REJECTED FAILED --------------------------------------------- sample of output file: M12345678 John Doe 11/20/15 M987654 REJECTED FAILED use strict; my $find = '^M'; open (NEW, ">", "output.txt" ) or die "could not open:$!"; open (FILE, "<", "Report.txt") or die "could not open:$!"; while (<FILE>) { print NEW if (/$find/); } close (FILE); close (NEW);

Replies are listed 'Best First'.
Re^4: copy line with character
by stevieb (Canon) on Nov 23, 2015 at 19:37 UTC

    You can use a variable to check whether you've made a match on the previous line or not, then reset it back to zero when you hit one (ie. printed the 2nd line). Note how the check for $match and the subsequent printing of the 2nd line is done before looking for $find and printing the first...

    my $match = 0; while (<FILE>) { # get rid of newlines chomp; if ($match){ # we have a line to print; reset the counter $match = 0; # replace multi-spaces with a single one s/\s+/ /g; print NEW "$_\n"; } if (/$find/){ # we found a $find; set $match to true $match = 1; print NEW $_; } }

    I'd recommend that you use lexical (variable-based) file handles instead of bareword handles though. BAREWORD handles are global, whereby variable based ones are lexical to the scope they're defined in, and can be passed around in subroutine calls:

    open my $fh, '<', 'Report.txt' or die "can't open the damned Report.txt file!: $!";

      Thanks again! Exactly what I was looking for! I really like what Perl is capable of doing as this makes simplifying files easier than copy and pasting. I had a friend teaching me but he ended up moving out of state. :( Glad I found this website.

        Perl does stand for (by some accounts, including the creator, Larry Wall): "Practical Extraction and Reporting Language". Another nickname is "Pathologically Eclectic Rubbish Lister"... so yep, it was designed for this kind of work from day one!

Re^4: copy line with character
by BillKSmith (Monsignor) on Nov 23, 2015 at 20:44 UTC
    Your sample is too small to tell if this would work. You may be able to greatly simplify your code by defining the special variable $RECORD_INPUT_SEPARATOR ($/) to redefine 'line' as a 'logical block' of input. See perlvar
    Bill

      Hi Bill, thank you... I was able to get my answer from Stevieb. But I do have another question if you don't mind. for my $find, I want the lines beginning with A or C.

      input sample: A123 A321 B123 B456 C321 C123 C654 output sample: A123 A321 C321 C123 C654

      Im sure my code is wrong below but that is what I am trying to accomplish, locating the lines beginning with A or C

      use strict; my $find = '^A' OR '^C'; open (NEW, ">", "output.txt" ) or die "could not open:$!"; open (FILE, "<", "Report.txt") or die "could not open:$!"; while (<FILE>) { print NEW if (/$find/); } close (FILE); close (NEW);
        Untested: my $find = qr/^(?:A|C)/;
        Sorry for my late reply. Others have already answered you latest question (use the syntax for a regular expression). You seem to have misunderstood my previous post. Stevieb's solution is correct and will work for almost any imaginable input. A much simpler solution may be possible depending on the format of the data file.
        Bill