How printf handles empty variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How printf handles empty variables
# 1  
Old 04-24-2008
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 problem is that if any of those happens to be empty, the ":" is not printed either, so that record is off by one field.
For example my input file has a populated $Comment (6th field) in each of the first two records , but it is empty in the second two records. This throws off my output file by one field.

How do I force it to print ":" so my records will print correctly?

I can get around it by using a test to see if it is empty and populating it but that seems cludgy to me.

Thanks,
SyyyBuggSmilie
# 2  
Old 04-24-2008
Put the variables between double quotes :
Code:
printf '\n:%2s:%10s:%15s:%15s:%10s:%15s:%1s:%2s:%2s:%10s:%2d:%2d:%1d:%1d:%1d:%1d:%1d:%1d:%1d:%3d' \
          "$Stat" "$Update" "$ip" . . .

Jean-Pierre.
# 3  
Old 04-24-2008
Jean-Perre,
That worked! Thanks,
SkyyBuggSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

The Way Systemd Handles System Calls

Hi everyone, I have a question about the process management, and deep level system functionality of system calls between SystemD and SystemV? Does SystemD use the same system calls (fork(), exec(), bind() etc...) as SystemV? or Vice Versa? If they both use the same or very very similar sys... (2 Replies)
Discussion started by: Lost in Cyberia
2 Replies

2. 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

3. UNIX for Dummies Questions & Answers

Check if file is empty with variables, bash

Hello again! I have some trouble with scripting in bash. In the following script I read from a folder with the files line0_Ux.xy line1_Ux.xy line2_Ux.xy . . . Some of the files are empty. For those I would like to print a "0" in list. I think the problem with the code is that... (4 Replies)
Discussion started by: bjoern456
4 Replies

4. Shell Programming and Scripting

Expect Script - Variables are Empty immediately after "Setting" Them?

Hello All, I have embedded some expect code inside a Bash script I'm writing, but for some reason any variable I 'set' to something is showing as empty immediately on the next line... I haven't run into this problem before so I'm not sure what it could be...? I'm guessing it has something to... (4 Replies)
Discussion started by: mrm5102
4 Replies

5. UNIX for Dummies Questions & Answers

Script to leak file handles

This is a strange one. We have an issue where our system is leaking SCTP file handles. There are people working on this and in the mean time we have a monitoring script that alarms when we need to perform actions to manually clear them. For testing purposes I want to write a script that... (0 Replies)
Discussion started by: RECrerar
0 Replies

6. 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

7. Shell Programming and Scripting

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: 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;... (2 Replies)
Discussion started by: origamisven
2 Replies

8. 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

9. 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

10. UNIX for Advanced & Expert Users

File Handles

Hi, perhaps you can answer my question.....;) How can I check, how many file handles are used and how can i increase the value for maximum file handles??? (3 Replies)
Discussion started by: sjaeger
3 Replies
Login or Register to Ask a Question