I have a code which is intended to output numbers stored in a file (which are in one column) to another TXT file. The part of the code which does this this is:

awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt >> row.txt

the output is something like this:

1.31    2.3    3.35    2.59    1.63
2.03    2.21    1.99    1.5    1.12 
1    0.6    -0.71    -2.1    0.01 

But I want it to be like this:

1.31    2.30    3.35    2.59    1.63
2.03    2.21    1.99    1.50    1.12 
1.00    0.60   -0.71   -2.10    0.01 

As you see all numbers in the second sample have 2 digits after decimal and also if they are negative, the negative sign is placed before the number so it doesn't mess the arrangement of the numbers.

Any idea?

P.S.: The input file is a text file with a column of numbers (for each row):

1.31
2.3
3.35
2.59
1.63

The whole code is like this:

#!/bin/sh

rm *.txt
for time in 00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96; do
    filename=gfs.t00z.master.grbf$time.10m.uv.grib2
    wgrib2 $filename -spread $time.txt
    sed 's:lon,lat,[A-Z]* 10 m above ground d=\([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*:\1 '$time'0000:' $time.txt > temp.txt
    for (( j = 1; j <= 2; j++ )); do
        if [ j == 1 ]; then
            sed -n '/lon,lat,UGRD/,/lon,lat,VGRD/p' $time.txt > vel_sep.txt
        else
            sed -n '/lon,lat,VGRD/,/359.500000,90.000000/p' $time.txt > vel_sep.txt
        fi
        line=174305
        sed -n 1p temp.txt >> row.txt
        for (( i = 1; i <= 48;  i++ )); do
            sed -n "$line","$(($line+93))"p vel_sep.txt > col.txt
            sed 's:[0-9]*.[0-9]*,[0-9]*.[0-9]*,::' col.txt > col_trim.txt   
            awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt >> row.txt
            line=$(($line-720))
        done
    done
done

exit 0
有帮助吗?

解决方案

Replace your awk by this:

awk -F"\n" 'NR==1{a=sprintf("%10.2f", $1); next}
           {a=sprintf("%s%10.2f", a,$1);}END{print a}' col_trim.txt >> row.txt

EDIT: For left alignment:

awk -F"\n" 'NR==1{a=sprintf("%-8.2f", $1); next}
           {a=sprintf("%s%-8.2f", a,$1);}END{print a}' col_trim.txt >> row.txt

其他提示

You can use the column command:

awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt | \
    column -t >> row.txt

This gives:

1.31  2.3   3.35   2.59  1.63
2.03  2.21  1.99   1.5   1.12
1     0.6   -0.71  -2.1  0.01

This can be solved using printf with awk

Eksample:

echo -e "1 -2.5 10\n-3.4 2   12" | awk '{printf "%8.2f %8.2f %8.2f\n",$1,$2,$3}'
    1.00    -2.50    10.00
   -3.40     2.00    12.00

Additionally, this script has big spaces we can improve.

Here is the first one:

change from:

for time in 00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96; do

to

for time in $(seq 0 3 96); do
    time=$(printf "%02d" $time)

if you can show us the sample output of wgrib2 $filename -spread $time.txt, we can give more suggestions.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top