1

I need to print the following values with printf as the follwoing around like this:

printf "[`date +%d"/"%b"/"%G"-"%T`] [WARN] $PARAM1 $PARAM2 $PARAM3 

The required output:

[02/Jun/2010-11:08:42] [WARN] val1....val2...val3 

the gap between val1 to val2 and from val2 to val3 must be const gap not depend the length of the values

1
  • 2
    This looks more like BASH than C. Commented Jun 2, 2010 at 10:26

2 Answers 2

1
printf "%s [WARN] %s %s %s\n" `date +"%d/%b/%G-%T"` foo bar baz 

Not sure what you meant by constant gap. If it is a column width for foo, bar and baz, try %13s, where 13 is minimum column width.

Sign up to request clarification or add additional context in comments.

1 Comment

waht about the new line in the printf sytax?
1

I understand your question. Using another answer as a base for mine:

If you want to pad each of the PARAMs then just add a numerical argument to the printf and it will pad it out to that number of characters per field.

Pad by 20 characters: printf "%s [WARN] %20s %20s %20s" date +"%d/%b/%G-%T" foo bar baz

Examples:

printf "%s [WARN] %16s %16s %16s" [`date +"%d/%b/%G-%T"`] foo bar baz 02/Jun/2010-11:22:54 [WARN] foo bar baz 

Longer...

printf "%s [WARN] %16s %16s %16s" [`date +"%d/%b/%G-%T"`] longerfoo longerbar longerbaz 02/Jun/2010-11:23:42 [WARN] longerfoo longerbar longerbaz 

Much Longer...

printf "%s [WARN] %16s %16s %16s" [`date +"%d/%b/%G-%T"`] muchlongerfoo muchlongerbar muchlongerbaz 02/Jun/2010-11:24:12 [WARN] muchlongerfoo muchlongerbar muchlongerbaz 

Try it on a console. It works.

3 Comments

Looks like hudolejev added the bit about column width while I was writing this one... You have two identical answers for the price of one! Woohoo!
hi again according to your example I get the follwoing , but the upper PARAM word not on the same side of the lower PARAM? [WARN] PARAM=oooee FILE=xxx MACHINE_NAME=uuu [WARN] PARAM=pppp FILE=lll MACHINE_NAME=ttt
@yael that's because strings get right-justified by default. To get them left-justified, use "%-16s".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.