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 ----printf's usage + awk-in clause

Status
Not open for further replies.

micotao

Programmer
Jun 16, 2004
33
CN
Hi, All:
Please check this script, to see what's the problem, I think it is right, but the running result makes me sad!!
(1)the source code
#!/bin/ksh
LDIF_LIST="/tmp/myfile"
for item in ${LDIF_LIST}
do
if [ -f ${item} ]; then
crtfile=`basename ${item}`
cp ${item} /tmp/${crtfile}.tmp.$$ 2>&-
cat /tmp/${crtfile}.tmp.$$ |
awk '{if (substr($0,1,1)!=" ") { printf "\n"; printf "%s" $0 }
else
if (substr($0,2,1)==" " ) { printf "%s", substr($0,3,10000) }
else { printf "%s", substr($0,2,10000) }
}END{printf "\n" }' | sed "s/host/room/g" > ${item}

fi
done


(2)the result when I run the -vx to debug:
awk: + 1>/tmp/myfile
not enough arguments in printf(%s)
record number 1

I THINK THERE IS NO PROBLEM WITH MY printf usage,but why it said "not enough arguments in printf(%s)"???
AND HOW SHOULD WE USE if-else statement in AWK ???Is there anything wrong???

THANKS A LOT! I AM CONFUSED!
 
And what about something like this ?
#!/bin/ksh
LDIF_LIST="/tmp/myfile"
for item in $LDIF_LIST; do
tmpitem=/tmp/$(basename $item).$$
[ -f $item ] && cp $item $tmpitem || continue
awk '
{gsub(/host/,"room")}
substr($0,1,1)!=" "{printf "\n%s",$0;next}
substr($0,2,1)==" "{printf "%s",substr($0,3);next}
{printf "%s",substr($0,2)}
END{printf "\n"}
' $tmpitem > $item
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top