注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

Function printf in AWK  

2015-08-27 15:36:08|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Function printf  outputs the formated data to standard output device. The usage of printf () is

printf ("format list\n", par1, par2, par3, ..., parn)

and the command of awk can be given by
awk '{printf ("%8.4f    %8.4f\n", $1, $2)}' input
For example, the data input lists below

38.8291 12.0796 0.2428 82.9826
46.0327 12.0796 0.2488 67.1277
51.7319 12.0796 0.2449 58.4459
55.6356 12.0796 0.2416 53.2473
56.0294 12.0796 0.2850 50.3007
56.6463 12.0796 0.3303 47.1758
57.5376 12.0796 0.3427 44.4151
58.0767 12.0796 0.3480 42.3626
58.3436 12.0796 0.3501 40.8966
58.4462 12.0796 0.3512 39.8569
58.4535 12.0796 0.3525 39.1044

If we execute the command

awk '(printf ("%12.4f %12.4f\n", $1, $2)}' input

we may obtain
     38.82910000         12.07960000
46.03270000 12.07960000
51.73190000 12.07960000
55.63560000 12.07960000
56.02940000 12.07960000
56.64630000 12.07960000
57.53760000 12.07960000
58.07670000 12.07960000
58.34360000 12.07960000
58.44620000 12.07960000
58.45350000 12.07960000

The space between two columns may be formated with the form %4s, by which 4 characters width of space may be obtained. So that the above command may also be given by
awk '(printf ("%12.4f%4s%12.4f\n", $1, "", $2)}' input
The %4s string means that the "" string occupies 4 space characters.

The formatting characters are derived from c language,  and which  listed below

%d integer
%u unsigned integer
%f float
%s string
%c character
%p pointer
%e exponential float
%x, %X unsigned hexadecimal integer
%0 unsigned octal integer
%g auto match
\n new line
\f clear the screen and change page
\r return
\t tab char
\xhh ASCII code given by a hexadecimal
  评论这张
 
阅读(130)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017