help with columns and rows - script

Hi everyone,

how can I convert a file with 3375 rows and 6 columns to a file with
1350 rows and 15 columns
by using a script?
Is it possible with awk or something like that?
Any help is much appreciated.

Thanks.
D.

Hmm. Something like this? It saves columns for later and prints them when it has enough.

awk \
'BEGIN { C=1; MAX=15; }
{
        for(N=1; N<=NF; N++)
        {
                B[C++]=$N;
                if(C > MAX)
                {
                        printf("%s", B[1]);
                        for(M=2; M<=MAX; M++)
                                printf("\t%s", C[M]);
                        printf("\n");
                        C=1;                       
                }
        }
}
END {
    if(c > 1)
    {
        printf("%s", C[1]);
        for(N=1; N<C; N++) printf("\t%s", C[N]);
        printf("\n");
    }
}'

any restrictions on layout/format ? i.e. do you want the first row of o/p to be the first 2.5 rows of input, 2nd row next 2.5 etc ?

Anyway...assuming fields are space seperated this random hack:

 tr " " "\n" <file | paste -d" " - - - - - | paste -d" " - - -

should work...

POSIX shell

#!/bin/sh
c=0
for f in `cat tmp.file`; do
    printf "$f "
    c=$((c + 1))
    [ $(($c % 15)) -eq 0 ] && echo
done
  1. This is a combo useless use of cat and useless use of backticks. Loading an entire file into a variable to chop it like this is especially dangerous since, if the file is larger than the maximum size of a shell variable, the end will get chopped off.
  2. You shouldn't feed variables into printf's command string unless you can control exactly what they are. It will throw up on or mangle anything with % or \ in it.
  3. It ignores every fifteenth cell.
  4. It may end up printing the last line without an ending line-feed, which may prevent the last line from being read by various UNIX utilities like sed and awk.
  5. It would require a KSH or KSH-like shell, for the $(( )) math syntax.
c=0
MAX=15
PREFIX=""
# read lines
while read f
do
        # split lines into $1, $2, ...
        set -- $f
        for d in $*
        do
                if [ `expr $c % $MAX` -eq 0 ]
                then
                        # PREFIX being blank prevents extra blank line on the very first line
                        printf "${PREFIX}%s" "$d"
                        PREFIX="\n"
                else
                        printf " %s" "$d"
                fi

                c=`expr $c + 1`       
        done
done < tmp.file

echo

Thanks everyone for replying so quickly!

I think yazu's answer is very close, however the script "skips" always the value "-9999.0" which I would like to keep, and does not "keep" the 15th value. Corona88 actually made exactly what I wanted to do.

Thank you all guys for your help!!!:b:

D.