awk printf

Posted on 2013-06-06
Medium Priority
Last Modified: 2013-06-07
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
echo   'Loading  load_schedule  in ods  started'
businessdate=`echo "set heading off
set pagesize 0
set feedback off
from control " \
|sqlplus -s $odsuser/$odspass |awk '{printf"%s",$0}'`
echo $businessdate

Open in new window

Question by:d27m11y
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2

Expert Comment

ID: 39226267
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.

Expert Comment

ID: 39226667
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)

Author Comment

ID: 39226891
printf %s - is this printing the string ?
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Expert Comment

ID: 39226903
Yes :)

Author Comment

ID: 39226937
$0 - does this get entire previous select statement in the shell ?
LVL 19

Accepted Solution

simon3270 earned 1000 total points
ID: 39227284
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.
LVL 19

Assisted Solution

simon3270 earned 1000 total points
ID: 39227291
"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
LVL 48

Expert Comment

ID: 39228307
The awk statement is redundant, as you could just do

echo -n $businessdate

LVL 19

Expert Comment

ID: 39228417
"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.

Author Closing Comment

ID: 39229442
Helpful !

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question