Changing integer columns to floating decimal columns

I have a document that has 7 columns.

eg.

$1      $2     $3     $4    $5        $6       $7
string string string string integer integer integer

The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all values are floating decimals w/4 decimal places, while leaving the rest of the worksheet in tact. In all I have 96 documents that need this conversion.

I have been able to output an individual column to a new worksheet in the right format with:

awk <input_filename ' { printf ("%.4f\n",$7) }' > output filename

However, I don't want to make 2 separate files and concatenate the original, plus the 2 new files and then awk out the columns I need on 96 files.

Thanks

If you don't mind the reformatting of the whitespace to a one-space:

awk <input_filename '{ $6=sprintf ("%.4f",$6); $7=sprintf ("%.4f",$7); print }' > output filename 
awk '{ for(N=6; N<=7; N++) $N=sprintf("%.4f", $N); print > FILENAME".new" }' OFS="\t" file1 file2 file3 file4 file5

This should make file1.new, file2.new, file3.new, file4.new, file5.new. You can't just overwrite your input while you're using it, and you shouldn't destroy your input files until verifying the output is what you want.

That works perfectly! Thanks so much :smiley:

1 Like