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.
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
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!!!
D.