1. 首页
  2. IT资讯

AWK截取字符串

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 formsubstr(s, a, b)and returnsbnumber of chars from strings, starting at positiona. The parameterbis 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 returnsE
nawk ‘{print substr($1,3)}’ temp returnsery
nawk ‘{print substr($2,3)}’ tempreturnsod
nawk ‘{print substr($0,7,2)}’ tempreturnsgo
分类:linux shell

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

主题测试文章,只做测试使用。发布者:布吉卡,转转请注明出处:http://www.cxybcw.com/196085.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code