LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk multiple column into single column (https://www.linuxquestions.org/questions/programming-9/awk-multiple-column-into-single-column-819931/)

ilukacevic 07-19-2010 03:13 PM

Quote:

Originally Posted by MTK358 (Post 4038463)
I don't understand the output format. What exactly is happening and what are those dots doing?

I apologize for not making the output more clear.

I should take column (141 rows) after column (6 of them) from the input file, put the next one under the previous one. That would be the 2nd column of the output. In the 1st column of the output should the line numbers go (141 of them for each column -> 6 times like that => 846 rows in total in the output file and 2 columns).

Dots are data not reprezented in my post...there is a lot of data, so I truncated the input file, without loosing the main features.


thnx for the effort!

Igor

MTK358 07-19-2010 03:44 PM

Still quite confused.

Also, do you still want some multiplication to happen?

colucix 07-19-2010 03:47 PM

Ok.. it's clear now. I will suggest my previous awk code (see post #23) with a little modification to specify the format of the numeric results. I'm sure grail and MTK358 will refine their suggestions, as well.
Code:

BEGIN { OFMT = "%.6f"; factor = 219474.6306726 }

{
  gsub(/D/,"E")
 
  for ( i=1; i<=NF; i++ )
    array[++count] = $i
}

END {
  for ( i=1; i<=count/NR; i++ ) {
    for ( j=0; j<NR; j++ ) {
      print j+1, array[i+j*NF] * factor
    }
  }
}

This is independent from the number of rows and columns. The OFMT variable controls the format of the numeric output with the print statement.

Last but not least, two advices for good awk readings:
1. the official GNU awk manual: http://www.gnu.org/software/gawk/manual/
2. the grymoire's awk tutorial: http://www.grymoire.com/Unix/Awk.html
Hope this helps! :)

MTK358 07-19-2010 04:01 PM

Code:

#!/usr/bin/env perl
use warnings;
use strict;

my $filename = $ARGV[0];
open my $file, $filename or die "error: $filename: could not open";

for my $col (0 .. 5) {
    my $linenum = 0;
    seek $file, 0, SEEK_SET;

    while (<$file>) {
        $linenum++;
        my @cols = split;
        print "$linenum" . $cols[$col] * 219474.6306726 . "\n";
    }
}


grail 07-19-2010 07:23 PM

Well mine and colucix's are just variations on a theme, but just so you have other stuff to look at :)
Code:

#!/usr/bin/awk -f

BEGIN { OFMT = "%.6f"; factor = 219474.6306726 }

{
    gsub(/D/,"E")

    for(i=1;i<=NF;i++)
    {
        if(arr[i])
            arr[i]=arr[i]"\n"

        arr[i]=arr[i]NR" " $i * factor
    }
}

END{
    for(x=1;x<=NF;x++)
        print arr[x]
}



All times are GMT -5. The time now is 07:20 AM.