Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk-printf-substr

Status
Not open for further replies.

micotao

Programmer
Jun 16, 2004
33
CN
Dear all:
I find it strange when I try this under the Solaris9-bash shell.
why
$-prompt) awk '{ if (substr($0,1,1)==" ") printf "%s" substr($0,2,100) }' file_my
run output is this:
awk: not enough arguments in printf(%sXXXXABCD0000 abc) or sprintf(%sXXXXABC
D0000 abc)
record number 1

The result "XXXXABCD0000 abc" is just what I want,but why do it say "argument not enough"??????

 
Replace this:
printf "%s" substr($0,2,100)
By this:
printf "%s"[highlight],[/highlight] substr($0,2,100)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank PHV! I have used the , again, with a " "(blank) between the "%s" and the substr($0,2,100), it seems work.
But why? When I try this:
printf "%s" abcd
output> abcd
but when I try this:
printf "%s", abcd
output> ,abcd
which is not what I want.
So can you tell what your opinion is?
 
see man printf, sscanf ...

awk's printf works exactly like c printf does.
this function is defined:
int printf(const char *format, /* args */ ...);
so you need a format and according to it, arguments

printf "%s" abcd
# must give an error

printf "%s" , abcd
# prints the string 'abcd'
# also NOTHING because 'abcd' is NOT defined,
# c will refuse to compile, awk is very tolerant

printf "%s" , "abcd"
or:
x = "abcd"; printf "%s", x
# prints 'abcd'

NOTA: i like to write function in the classic
way, even if (syntactically) NOT required, this
is a big help reading code:
printf("%s\n",x);



:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top