in reply to Insert Text which is Read from the Line Above

perl -ane "@F<3 and @hdr=@F,next; print qq|@hdr $_|" data1.txt
Change to single quotes for Linux.

        "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

Replies are listed 'Best First'.
Re^2: Insert Text which is Read from the Line Above
by ExReg (Priest) on Feb 26, 2016 at 20:54 UTC

    And that is why I love Perl! I'd give you my whole days worth of ++ if I had more left. A pure work of art!

Re^2: Insert Text which is Read from the Line Above
by jlb333333 (Novice) on Feb 27, 2016 at 08:15 UTC

    Hi NetWallah.

    That is absolutely amazing.

    As I am part of the below average person group you will have to help me with one further question.

    I managed to capture the output through running the command on Explorer^2's DOS Command line option.

    I pasted the output into a text file and all my checks so far on your output after running the code on a 2600 line input file have found the output to be 100% correct.

    However, I need to capture the output in a more easy way.

    How do I insert this code into a .pl file? Do I need 'if ($line =~ ........' etc.

    If you can give me some idea on that it will help me so much for this particular program and many more in the future.

    Many, many, many thanks.

      #!perl use strict; my $infile = $ARGV[0]; my $outfile = $ARGV[1]; unless (@ARGV==2) { die "Usage : perl $0 infile outfile\n" }; open IN, '<',$infile or die "Could not open $infile : $!\n"; open OUT,'>',$outfile or die "Could not open $outfile : $!\n"; my @hdr; my $count_in; my $count_out; while (<IN>){ ++$count_in; my @f = split; if (@f<3){ @hdr = @f; } else { print OUT "@hdr $_"; ++$count_out; } } print "$count_in lines read from $infile\n"; print "$count_out lines written to $outfile\n"; close IN; close OUT;
      poj

        Hi poj.

        Thank you for your assistance.

        You guys/gals are advanced here.

        I replaced the  my $infile = $ARGV[0]; with  my $infile = C:\Users\... \abc.txt;

        Similar with the output file.

        I knew it was not right to do this but I did not know what else to do.

        How do I get my input file read in and the output into the output file?

        Again, thank you very much for the code and assistance.

        jlb333333