Please help this problem.
I have this command:
awk 'BEGIN {printf "%-15s", "Date of birth:" > "/dev/stderr"; getline var; print "Today finished:", var ,"days"}'
How do I print ‘var’ with only the first command ‘printf’? Can I remove the second command ‘print’?
Thank you for your help.
EDIT-1:
answer for Mat
The first example is good, but prints ‘var’ in the next line. Is it possible to print the ‘var’ in the same line?
EDIT-2:
answer for Peter.O
Thank you for your help. Exactly the point. Data will be used for further calculations. Result of the calculation will be printed in the same line. Example:
awk 'BEGIN {printf "%-15s", "Date of birth:" > "/dev/stderr"; getline var; print "Today finished:", var ,"days"}'`
That I want to get the result:
Date of birth: 2011-02-23 Today finished: 2011-02-23 days
EDIT-3:
answer for shellter
Thank you for your comments.
In your example code, you have redirected
printfto stderr. I assume that is because you don’t want"Enter the date:"going to the final output (stdout) which will be further processed … That works and is a clean way of disposing of that unwanted text.So if you want your formatted output:
Date of birth: 2011-02-23 Today finished: 2011-02-23 daysto go to standard output (stdout), then the answer to your question is simple: You definitely need to call another instance of
print/printfwhich is directed to standard out.print/printfhas no concept of where its output is going; it just prints … The redirection operator>has no connection whatsoever to the internal workings of print(f), so you can’t get a single call of print(f) to send some output tostderrand the rest tostdout.