fieldwidths, printf and calculations with variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fieldwidths, printf and calculations with variables
# 1  
Old 11-15-2011
fieldwidths, printf and calculations with variables

Hello guys,
I have a problem concerning the formatting when performing calculations with variables passed to awk and using fieldwidth definitions and printf.

I have the following code:
Code:
awk 'BEGIN{ FIELDWIDTHS = "5 3 7 5 8 8 8" }{if ($7 != "") printf"%s%-s%s%s%s%s%s\n",$1,$2,$3,$4,$5,$6,$7; else print $0}' input > output

This prints out the desired output formatting of the file, which looks something like this (can't quite get the formatting correct with the forum tools):

Code:
CALB_mutant
168875
    1ARG      N    1   4.264   6.256  16.286
    1ARG     H1    2   4.350   6.308  16.282
    1ARG     H2    3   4.188   6.320  16.270
    1ARG     H3    4   4.267   6.249  16.387
    1ARG     CA    5   4.257   6.123  16.220
    1ARG     HA    6   4.337   6.062  16.262
    ...

However, as soon as I try to make changes to some of the column values, the formatting doesn't work for columns that were changed:

Code:
awk -v X0="$X0" -v Y0="$Y0" -v Z0="$Z0" 'BEGIN{ FIELDWIDTHS = "5 3 7 5 8 8 8" }{if ($7 != "")  printf"%s%-s%s%s%s%s%s\n",$1,$2,$3,$4,$5-X0,$6-Y0,$7-Z0; else print $0}' input > output

Instead the output looks something like this...

Code:
CALB_mutant
168875
    1ARG      N    1-8.2123.199815.5585
    1ARG     H1    2-8.1263.251815.5545
    1ARG     H2    3-8.2883.263815.5425
    1ARG     H3    4-8.2093.192815.6595
    1ARG     CA    5-8.2193.066815.4925
    1ARG     HA    6-8.1393.005815.5345
    ...

The values in the last three columns have to be right indented in fields of size 8, because values like -999.999 need to be supported.

Any help is highly appreciated...
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-15-2011 at 11:50 AM.. Reason: formatting and typos
# 2  
Old 11-15-2011
You're supposed to use [code] tags. Formatting works fine inside them.

---------- Post updated at 09:50 AM ---------- Previous update was at 09:47 AM ----------

Try %8s instead of %s for fields you want padded with spaces up to eight characters.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-15-2011
Of course, thank you... I guess I need to go home and get some sleep Smilie

Code:
awk -v X0="$X0" -v Y0="$Y0" -v Z0="$Z0" 'BEGIN{ FIELDWIDTHS = "5 3 7 5 8 8 8" }{if ($7 != "") printf"%s%-s%s%s%8.3f%8.3f%8.3f\n",$1,$2,$3,$4,$5-X0,$6-Y0,$7-Z0; else print $0}' input > output

for reference: when editing .gro coordinate files with the correct output format, this works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing printf formatting parameters as variables

Hi, I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy. :o That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to... (7 Replies)
Discussion started by: mph
7 Replies

2. Shell Programming and Scripting

FIELDWIDTHS problem

Hello; i have a file contains records such as below, i want to AWK read pre-defined fixed column width and print desired output. so i wrote this command: input: Event 9477887 Western Iran Date Time Err RMS Latitude Longitude Smaj Smin Az Depth Err Ndef Nsta Gap mdist ... (4 Replies)
Discussion started by: saeed.soltani
4 Replies

3. Shell Programming and Scripting

assinging the printf result to variables

Getting the below error while executing this. Able to run the below commands Individually. #!/bin/bash a=$(printf "%d\n" 0x01E); b=$(printf "%d\n" 0x01A); echo $a echo $b c=`expr $a - $b` echo $c syntax error at line 2: `a=$' unexpected (2 Replies)
Discussion started by: sai_1712
2 Replies

4. UNIX for Dummies Questions & Answers

help with doing calculations on data

Dear All, I have a long list like this: 337 375 364 389 443 578 1001 20100 . . . . etc I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like 337 0 (10 Replies)
Discussion started by: pawannoel
10 Replies

5. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

6. Shell Programming and Scripting

awk{FIELDWIDTHS} replacing blanks with null

Hi, I am having a file and grabbed the contents of the field according to field widths. The command i used is: awk 'BEGIN{FIELDWIDTHS="10 25 20 14 6 10"}{print$4,$5,$6}' newtext.text >test1.txt i got the output for example: val1 val2 val3 <blank> ... (3 Replies)
Discussion started by: rish_max
3 Replies

7. Shell Programming and Scripting

using AWK printf with userdefine variables

seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf i've come up with the following, but it's not working.:mad: --- mydatafile.dat e.g. of data in file: cau_actvty_fa_lrf.ksh fan_soco_fa.ksh ny_sum_lst.ksh... (4 Replies)
Discussion started by: danmauer
4 Replies

8. Shell Programming and Scripting

ksh, calculations using bc

hi all, was wondering if there is another way to do calculations in ksh scripts other than using bc ?? i am using a script to calculate average response time and my script errors out after running for a bit. e.g code i am using : averageTime=$(print "$totalTime / $numberOfEntries" |... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

How printf handles empty variables

See attached file that includes code, input and output. I am processing a colon delimited input file and building a ":" (colon) delimited output file. My printf statement prints contents of each of 20 variables and puts them into a record file. I am pushing out approximately 120 records. The... (2 Replies)
Discussion started by: Skyybugg
2 Replies

10. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies
Login or Register to Ask a Question