GiJoe has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a text file that looks like this:
0.000000 #need to delete this line
0.000000 0.000000 0.000000 ..256 elements
0.000000 0.000000 0.000000 17.962402 10.630810 ..256 elements
repeats....

Question1: How can I separate every other 256 element into a new text file and leave a blank line or space between each 256 element?

Question2: How do I get rid of that pesky first line at the beginning of the text file: "0.000000 "

Here is how I started:

#!/usr/local/bin/perl open (OLD, 'old.txt'); open (NEW, '>new.txt'); while (<OLD>) { chomp; next unless $_ =~ /\t/ && /0.000000/; next unless $_ !~ "F"; $_ =~ s/\t//; print NEW ("$_\n"); } close (OLD); close (NEW);

Any feedback is greatly appreciated:-)

Replies are listed 'Best First'.
Re: Text Massage
by citromatik (Curate) on Apr 07, 2009 at 23:21 UTC

    This is what I understand from your explanation:

    • You want to get rid of the first line of the file
    • and for the rest of the lines, put each element in a separate line and add a blank after each original "row".

    This is some (untested) code to do this:

    open my $infh, "<", "old.txt" or die $!; open my $outfh, ">", "new.txt" or die $!; <$infh>; # Discard the first line while (<$infh>) { chomp; s/\t/\n/; print {$outfh} "$_\n"; } close $infh; close $outfh;

    I am asuming that the records are separated by tabs. The first line can also be discarded inside the while loop by using the $. variable (see perlvar: next unless $. == 1).

    If I haven't understand you correctly, please let me know

    citromatik

Re: Text Massage
by shmem (Chancellor) on Apr 07, 2009 at 23:21 UTC

    To get rid if the first line shoved through a file handle:

    <FH>; # FH is an already opened filehandle. You can use a variable, to +o.

    As for question number 1: count them. There's split for splitting a string into a list, which you can stuff into an array. See push. Keep tack of the number of elements. Evaluating an array in scalar context gives you the number of elements in an array. See perldata.

    Really, read all of the perl man-pages from top to bottom. Don't despair if you don't grab all things in there at first. Just read them. Things will assemble; ruminating they fall into place.

Re: Text Massage
by Marshall (Canon) on Apr 08, 2009 at 00:26 UTC
    Question #2 is easy.
    Can you explain Question #1 further?
    For something like this make a few rows with say 6 numbers each (like 0.0 12.1 6.5 2.3,etc...no need for full precision in the example). Show that input and then show desired output.
      #columns are tab delimeter
      What I have:
      0.000000 #delete this line
      0.000000 0.000000 0.000000 ..curve 1 256 elements
      0.000000 0.000000 0.000000 ..curve 2 256 elements
      0.000000 2.566058 1.466319 ..curve 1 256 elements
      2.199478 2.199478 3.665797 ..curve 2 256 elements
      pattern continues curve 1,2,1,2,1,2...

      What I need:
      output to text file "A"
      0.000000 0.000000 0.000000 ..curve 1 256 elements
      blank line
      0.000000 2.566058 1.466319 ..curve 1 256 elements
      blank line
      cont...

      output to text file "B"
      0.000000 0.000000 0.000000 ..curve 2 256 elements
      blank line
      2.199478 2.199478 3.665797 ..curve 2 256 elements
      blank line
      cont...
      -------------------------------------------------------
      I hope i explained better. This is what the original file looks like:
      -------------------------------------------------------
      Curve1: UBIN
      NumSamples: 1
      RepCode: 68
      SampleSize: 1024 NumElements: 256
      0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 2.566058 1.466319 2.199478 2.199478 3.665797 2.199478 2.932637 2.566058 3.299217 2.932637 5.498694 2.932637 4.398956 3.299217 2.199478 2.932637 3.299217 1.466319 2.566058 2.566058 2.932637 2.932637 0.733159 1.099739 2.566058 1.466319 1.832898 1.099739 0.733159 1.466319 2.199478 1.832898 1.832898 1.099739 2.199478 0.000000 0.733159 0.733159 1.466319 1.099739 2.199478 2.566058 3.299217 4.032376 4.765535 5.865274 6.965014 9.164492 11.730549 9.164492 9.897650 13.563448 6.231853 8.064753 5.498694 2.932637 2.199478 1.099739 1.099739 0.733159 0.733159 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.366580 0.000000 0.000000 0.366580 0.366580 0.000000 0.733159 0.366580 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.366580 0.366580 0.366580 0.000000 0.366580 0.000000 0.366580 0.366580 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

      Curve2: LBIN
      NumSamples: 1
      RepCode: 68
      SampleSize: 1024 NumElements: 256
      0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.733159 0.366580 1.099739 2.199478 3.665797 5.132115 6.231853 7.698173 9.531071 6.965014 11.730549 10.997389 17.962402 10.630810 15.029766 15.396345 8.797913 11.363968 16.496086 9.531071 12.830288 10.630810 14.663187 11.363968 13.563448 8.064753 10.630810 9.531071 13.196867 8.431332 10.264231 10.264231 5.132115 7.698173 14.296606 8.064753 8.797913 5.865274 7.698173 6.231853 7.331594 8.431332 10.264231 6.598433 8.797913 6.598433 7.331594 9.531071 7.331594 4.765535 7.698173 6.231853 7.331594 9.897650 8.431332 14.296606 18.328983 11.730549 13.930027 16.496086 11.363968 10.997389 7.331594 7.331594 5.132115 7.698173 4.765535 5.498694 2.932637 5.132115 4.765535 3.665797 5.865274 4.032376 2.932637 3.665797 5.132115 3.665797 4.398956 4.398956 5.498694 3.665797 2.566058 5.132115 4.398956 2.199478 1.832898 5.132115 5.498694 5.132115 3.299217 1.466319 5.132115 5.865274 4.765535 5.132115 3.299217 4.765535 7.698173 8.064753 8.797913 11.363968 9.164492 11.730549 13.930027 10.997389 9.897650 13.930027 10.630810 11.363968 7.331594 8.431332 6.965014 8.797913 3.299217 7.331594 10.264231 8.064753 9.164492 6.965014 6.598433 8.431332 5.498694 7.698173 9.164492 7.331594 3.665797 5.132115 4.398956 3.299217 1.832898 2.199478 1.466319 0.366580 0.733159 1.099739 0.366580 0.366580 0.366580 0.000000 0.733159 1.099739 0.366580 0.000000 0.000000 0.000000 0.366580 0.000000 0.366580 0.366580 0.000000 0.000000 0.366580 0.000000 0.000000 0.366580 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.366580 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.733159 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

      Frame readOk!!!
      Frame Depth = 2.000000
      Curve[0]: TIME
      NumSamples: 1
      RepCode: 68
      SampleSize: 4 NumElements: 1
      2.000000
      Curve1: UBIN
      NumSamples: 1
      RepCode: 68
      SampleSize: 1024 NumElements: 256
      0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.300065 2.566147 2.966686 3.999977 5.200147 5.166321 4.000022 4.733136 6.767208 6.900260 6.233482 9.399803 6.533770 5.899480 7.800454 6.100563 5.933439 5.700024 3.566949 4.366557 4.066492 7.133698 3.832987 2.233527 3.800410 4.066559 2.066582 2.433072 2.600263 1.933507 2.066537 3.399804 3.033201 3.633331 2.300065 3.699890 1.500435 2.533592 2.833679 3.566816 1.099784 4.299953 4.066492 4.499632 5.833010 9.866812 9.466408 13.266951 14.266081 20.432980 16.966843 20.400425 20.765869 13.433762 11.966038 9.999865 5.633375 3.399915 1.699891 1.699891 1.033246 1.333311 0.366602 0.300087 0.300065 0.300065 0.300065 0.000000 0.366602 0.366602 0.000000 0.600129 0.366580 0.366580 0.300065 0.733204 0.366580 0.366580 0.000000 0.000022 0.000022 0.000022 0.300109 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.300065 0.366580 0.300065 0.000000 0.000000 0.000000 0.000000 0.000022 0.000000 0.366580 0.000022 0.000000 0.366580 0.000000 0.000022 0.000000 0.000000 0.366580 0.000000 0.000000 0.300087 0.000022 0.000000 0.000022 0.366580 0.000000 0.666644 0.366580 0.666644 0.000045 0.366580 0.000000 0.666644 0.366580 0.366580 0.000000 0.000000 0.000022 0.000000 0.000000 0.000000 0.000000 0.000000 0.300065 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000022 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000022 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

      Curve2: LBIN
      NumSamples: 1
      RepCode: 68
      SampleSize: 1024 NumElements: 256
      0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.333311 0.666667 2.300042 3.700024 6.366556 7.833255 10.133074 12.499809 17.033291 13.266951 21.033066 24.201035 33.566589 21.734161 25.832939 27.700066 18.401161 22.767162 26.098801 21.834278 22.433250 20.233547 22.165562 20.066647 22.866184 20.067806 20.833652 18.833786 22.499474 16.233459 19.266682 17.166252 12.934196 14.000044 21.798801 14.666531 15.099737 11.867058 13.699978 13.433851 12.133028 15.333622 16.566124 9.599459 12.999331 11.700068 14.233324 14.332729 15.433651 12.267597 11.299438 11.033335 15.133898 21.300709 20.134590 24.799671 28.832115 25.234283 28.934151 26.999260 20.966640 19.400093 14.533703 15.433672 10.833946 11.299351 9.566948 9.399914 6.233593 9.633375 7.166321 6.066626 11.266773 7.333377 5.933685 7.867014 7.532811 5.766405 8.600241 8.300154 10.900082 6.666688 4.066537 9.033089 7.999887 3.700225 7.234463 6.332620 8.199432 6.932682 5.700047 3.567017 6.032577 8.866234 7.466295 8.733271 7.200325 7.166521 11.899435 13.166296 15.699888 20.366421 16.066647 19.533165 25.033131 21.800385 18.300308 19.331860 18.133095 17.666107 12.133341 13.533054 14.467209 14.499609 10.201239 15.733738 18.366467 14.366800 16.666622 14.767206 14.100786 16.533634 12.100451 13.699978 15.466206 12.733135 7.266952 8.133030 8.299974 7.800476 2.433161 3.099761 2.366624 0.666689 1.033268 1.399826 0.966731 0.666667 0.666667 0.300065 0.733204 1.399804 0.666667 0.000000 0.000022 0.000022 0.366602 0.000000 0.666644 0.366580 0.000000 0.300065 0.366624 0.000022 0.000022 0.366580 0.366580 0.000000 0.300065 0.300065 0.000000 0.000022 0.000000 0.000000 0.000022 0.300087 0.000000 0.000000 0.000000 0.300065 0.000000 0.666667 0.366580 0.000000 0.300065 0.300065 0.300087 0.366580 0.300065 0.000000 0.000022 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.300065 0.000022 0.000000 0.000000 0.000022 0.000000 0.000000 0.000000 0.366580 0.000000 0.000000 0.000000 0.000000 0.000000 0.366580 0.000000 0.000022 0.000000 0.000022 0.000000 0.000000 0.666644 0.000022 0.000022 0.000000 0.000000 0.000000 0.366580 0.000000 0.300065 0.300065 0.000000 0.000000 0.000022 0.366580 0.000022 0.000000 0.000000 0.733159 0.000000 0.000022 0.000000 0.366580 0.000000 0.000000

        I am still unsure as to what you want. To separate the blocks of number is easy.
        #!/usr/bin/perl -w use strict; $| =1; my $curve1_out = 'curve1.out'; my $curve2_out = 'curve2.out'; my $in ='curve12.data'; open (IN, '<', $in) or die "cannot open $in $!"; open (my $c1, '>', $curve1_out) or die "cannot open $curve1_out $!"; open (my $c2, '>', $curve2_out) or die "cannot open $curve2_out $!"; while (<IN>) { if (/^Curve(\d):/.../^\s*$/) { if (defined($1) && $1==1 && /^\d/) {print $c1 "$_\n";}; if (defined($1) && $1==2 && /^\d/) {print $c2 "$_\n"}; } }
        data attached...So what else needs to happen?