[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

awk printf

Posted on 2013-06-06
10
Medium Priority
?
734 Views
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
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

0
Comment
Question by:d27m11y
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:tsnirone
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.
0
 

Expert Comment

by:tskarp
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)
0
 

Author Comment

by:d27m11y
ID: 39226891
printf %s - is this printing the string ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:tsnirone
ID: 39226903
Yes :)
0
 

Author Comment

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

Accepted Solution

by:
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.
0
 
LVL 20

Assisted Solution

by:simon3270
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
0
 
LVL 48

Expert Comment

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

echo -n $businessdate

instead
0
 
LVL 20

Expert Comment

by:simon3270
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.
0
 

Author Closing Comment

by:d27m11y
ID: 39229442
Helpful !
0

Featured Post

Industry Leaders: 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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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