ITPub博客

首页 > Linux操作系统 > Linux操作系统 > AWK截取字符串

AWK截取字符串

原创 Linux操作系统 作者:perfychi 时间:2012-09-18 23:09:35 0 删除 编辑

AWK截取字符串

文件file的每一行的格式如下:

600001  200712  SH  20080413

希望吧格式调整为:

600001  2007  12  2008  04  13

可以实用以下shell命令:

cat file | awk '{print $1 "\t" substr($2,1,4) "\t" substr($2,5,2) "\t" substr($4,1,4) "\t"  substr($4,5,2) "\t" substr($4,7,2)}' > newfile

 Awk substr function

Let's look at the substr function of awk.
This function has the form substr(s, a, b) and returns b number of chars from string s, starting at position a.  The parameterb is optional.
 Assume that we have a one line file called temp:
 Every good boy.
 Then, here are some substr examples:
 nawk '{print substr($1,1,1)}' temp   returns E
nawk '{print substr($1,3)}' temp   returns ery
nawk '{print substr($2,3)}' temp   returns od
nawk '{print substr($0,7,2)}' temp  returns go
分类: linux shell

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27042095/viewspace-743984/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-06-06

  • 博文量
    484
  • 访问量
    2976219