How to print a string using printf?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print a string using printf?
# 1  
Old 06-14-2010
How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf.
If I try printing it using printf it is printing as follows.

output
-------
str1
str2
str3
str4

btw I'm working in AIX.

This is my first post in this forum Smilie

regards,
rakesh
# 2  
Old 06-14-2010
Quote:
Originally Posted by enigmatrix
I want to print a string say "str1 str2 str3 str4" using printf.
If I try printing it using printf it is printing as follows.

output
-------
str1
str2
str3
str4
If it's in the Bash shell -

Code:
$
$ STR="str1 str2 str3 str4"
$
$ printf "%s\n" "$STR"
str1 str2 str3 str4
$
$

tyler_durden

... Don't know if AIX has Bash shell.
# 3  
Old 06-14-2010
or
Code:
# printf "$STR\n"
str1 str2 str3 str4

# 4  
Old 06-14-2010
Quote:
Originally Posted by ygemici
or
Code:
# printf "$STR\n"
str1 str2 str3 str4

It's best not to use printf that way, since character sequences that are special to printf (format specifiers, escape sequences) will be misinterpreted and lead to mangled output. To print the value of a variable, it's best to use:
Code:
printf '%s\n' "$STR"


Example of how things would go wrong:
Code:
$ STR='%s'
$ # Incorrectly prints nothing but a newline
$ printf "$STR\n"

$ # Correctly prints out the value of $STR
$ printf "%s\n" "$STR"
%s

Using the printf(1) utility, this is nothing but a minor bug. Using the printf(3) function in the C standard library, this would be a HUGE security hole.

Regards,
Alister
# 5  
Old 06-16-2010
thanks guys the method suggested by durden_tyler and alister works.
@durden_tyler: I'm working in ksh Smilie not bash.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies

2. Shell Programming and Scripting

Combining awk printf and print strftime command

I have a lines like below, captured from rrdtool fetch command, 1395295200 2.0629986254e+06 7.4634784967e+05 1395297000 2.0198121616e+06 6.8658888903e+05 1395298800 1.8787141122e+06 6.7482866452e+05 1395300600 1.7586118678e+06 6.7867977653e+05 1395302400 1.8222762151e+06 7.1301678859e+05I'm... (3 Replies)
Discussion started by: rk4k
3 Replies

3. Programming

[Perl] Different printf formating for different print options

Hi, Struggling with single quotes, double quotes, etc. I want to print a header line, followed by lines with actual values, based on a print option. In real life it is going to be something like 15 print options and 50 values. Output will be 1 header and several value lines. In this example... (5 Replies)
Discussion started by: ejdv
5 Replies

4. Shell Programming and Scripting

How to combine print and printf on awk

# cat t.txt 2,3,4,5,A,2012-01-01 00:00:28 2,6,4,5,A,2012-01-02 00:00:28 2,7,4,5,A,2012-01-02 02:00:28 # awk -F"," '{OFS=",";print $2,"";printf("%s", strftime("%m%d%y",$6));printf("%s", strftime("%H%M%S \n",$6));print ("",$1)}' t.txt 3, 010170073332 ,2 6, 010170073332 ,2 7,... (3 Replies)
Discussion started by: before4
3 Replies

5. Shell Programming and Scripting

AWK Too many open streams to print/printf

hallow all i need your advice about this script i have script like this: INDEX=/zpool1/NFS/INDEX/${1} SCRIPT=/zpool1/NFS/script/${1} LIST=SAMPLE cd ${SCRIPT} for i in `cat ${LIST}` do GETDATE=`echo ${i}|awk '{print substr($1,9,8)}'` /usr/xpg4/bin/awk -F ":" '{close(f);f=$4}{print >>... (4 Replies)
Discussion started by: zvtral
4 Replies

6. Shell Programming and Scripting

What's the difference between print and printf in command?

For example, in this command: ls /etc/rc0.d/ -print ls /etc/rc0.d/ -printfThe outputs are quite different, why? (7 Replies)
Discussion started by: Henryyy
7 Replies

7. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

8. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

9. Shell Programming and Scripting

printf with Character String

I am trying to use printf with a character string that is used within a do loop. The problem is that while in the loop, the printf prints the variable name instead of the value. The do loop calls the variable name from a text file (called device.txt): while read device do cat $device.clean... (2 Replies)
Discussion started by: dleblanc67
2 Replies

10. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies
Login or Register to Ask a Question