Home » Infrastructure » Unix » Problem with AWK in formating the data
Problem with AWK in formating the data [message #140975] Thu, 06 October 2005 08:03 Go to next message
rajus19
Messages: 18
Registered: September 2005
Junior Member
Hi All,

I am using "awk" command to find the sum of the sal for all the emps. The emp data is available in a flat file (in_file). The sal field is from the 21st to 32nd character (right justified with Zeros).

I am using the below command to find the tatal sal, but its not giving the output in proper format.

total=`awk '{(tot_sal=tot_sal+ substr($0,21,12))} END {print tot_sal}' ${in_file}`

Your help will be greatly appreciated.

Thanks
Raju
Re: Problem with AWK in formating the data [message #140987 is a reply to message #140975] Thu, 06 October 2005 08:41 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
Use printf "%.12d\n", tot_sal

Best regards.

Frank
Re: Problem with AWK in formating the data [message #141006 is a reply to message #140987] Thu, 06 October 2005 10:24 Go to previous messageGo to next message
rajus19
Messages: 18
Registered: September 2005
Junior Member
Thanks Frank,

Could you please help me how to display the output in the format of right justified with spaces using awk.

Thanks
Raju
Re: Problem with AWK in formating the data [message #141175 is a reply to message #141006] Fri, 07 October 2005 06:37 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
I haven't tested it, but printf()'s output should be right-justified, unless you place a minus sign right after the % sign.

Best regards.

Frank
Re: Problem with AWK in formating the data [message #141177 is a reply to message #141175] Fri, 07 October 2005 06:43 Go to previous messageGo to next message
tarundua
Messages: 1080
Registered: June 2005
Location: India
Senior Member

just one more thing to add,

this printf() follows C-language formatting so get any book on C language and look into the formatting .
Re: Problem with AWK in formating the data [message #141203 is a reply to message #140975] Fri, 07 October 2005 08:34 Go to previous message
rajus19
Messages: 18
Registered: September 2005
Junior Member
Thank You very much Guys.

Regards,
Raju
Previous Topic: Blank Space problem in unix
Next Topic: DISPLAY setting for Oracle 9.2.0.6.0 on SunOs 5.8
Goto Forum:
  


Current Time: Thu Mar 28 20:52:50 CDT 2024