awk printf

In the below script, I wanted to know what is awk doing, is that printing the string and what is $0. Also, can we use print instead of printf here... what is the difference between these two.

#!/bin/ksh -xve
date
echo   'Loading  load_schedule  in ods  started'
businessdate=`echo "set heading off
set pagesize 0
set feedback off
select  DECODE(TRIM(SYSDATE),NEXT_BUSINESS_DATE, 'BUSINESS-DATE', 'NOT-BUSINESS-DATE' )
from control " \
|sqlplus -s $odsuser/$odspass |awk '{printf"%s",$0}'`
echo $businessdate

Open in new window

d27m11yAsked:
Who is Participating?
 
simon3270Connect With a Mentor Commented:
printf "%s", $0
will print each line in its input, without a trailing newline.  So, if the "SELECT" produces multiple lines, they would be printed all on one line.

I think someone has assumed that the trailing newline would be in the text assigned to the businessdate variable - in fact backticks swallow that trailing newline, so if there is only one line in the SELECT output, the entire awk sattement is not required.  If there are multiple lines, it does ensure that they are all on a single line in the businessdate variable.
0
 
tsnironeCommented:
If I am not mistaken (been very long since I scripted) it seems to me that it uses awk (and printf) to simply print the result from the select statement.
0
 
tskarpCommented:
The $0 in awk just print the entire row as it is. With printf you can convert and print each field with your format you like (decimal, string etc)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
d27m11yAuthor Commented:
printf %s - is this printing the string ?
0
 
tsnironeCommented:
Yes :)
0
 
d27m11yAuthor Commented:
$0 - does this get entire previous select statement in the shell ?
0
 
simon3270Connect With a Mentor Commented:
"print", on the other hand, *does* put a trailing newline after the text it writes, so mulitple SELECT output lines would appear as multiple lines in $businessdate.

To get newlines in printf, use \n, as in:
    printf "%s\n", $0
0
 
TintinCommented:
The awk statement is redundant, as you could just do

echo -n $businessdate

instead
0
 
simon3270Commented:
"echo -n" isn't required either - in all cases, $businessdate doesn't have a trailing newline because of the backticks.

As I said, the awk is only required if the sqplplus writes multiple output lines, and they need to be on a single line with no spaces between them - if spaces are OK, the awk can be left off.
0
 
d27m11yAuthor Commented:
Helpful !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.