免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1567 | 回复: 8
打印 上一主题 下一主题

求教 awk 重组问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-20 13:33 |只看该作者 |倒序浏览
当设定了OFS后 想要使用新的分隔符 一般都会使用类似 $1=$1,NF+=0

所以想问下 当awk 看到$1=$1 他是怎么来判断的或者说这个是怎么执行的

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2012-04-20 13:39 |只看该作者
Advanced Notes: Changing FS Does Not Affect the Fields

According to the POSIX standard, awk is supposed to behave as if each record is split into fields at the time it is read. In particular, this means that if you change the value of FS after a record is read, the value of the fields (i.e., how they were split) should reflect the old value of FS, not the new one.

However, many older implementations of awk do not work this way. Instead, they defer splitting the fields until a field is actually referenced. The fields are split using the current value of FS! (d.c.) This behavior can be difficult to diagnose. The following example illustrates the difference between the two methods.

论坛徽章:
0
3 [报告]
发表于 2012-04-20 13:41 |只看该作者
回复 2# waker


    你难到没看到楼主的名么
还在这粘英文

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
4 [报告]
发表于 2012-04-20 13:44 |只看该作者
dahaoshanhe 发表于 2012-04-20 13:41
回复 2# waker


俺也是英语盲,understand?

论坛徽章:
0
5 [报告]
发表于 2012-04-20 13:46 |只看该作者
回复 2# waker


    先谢了 不过我真看不懂

论坛徽章:
0
6 [报告]
发表于 2012-04-20 13:55 |只看该作者
回复 2# waker


    不过大体意思是如果没域内的值改变了 那么OFS就会应用新的吗?

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
7 [报告]
发表于 2012-04-20 13:56 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2012-04-21 08:52 |只看该作者
回复 1# 英语盲学linux


    我看manua时候,是说每当一个record读进来的时候, 根据 FS 把每个 $1, $2...$NF 的值确定, 同时根据当时OFS也确定了输出样式。

只读取record 或 field 值是不会对已经确定的东西产生影响,但写就不一样了。

如果改变了某一个 field 的值, 比如 $1=$1,(其实把任何值赋给任何 field 都会这样) $1值的改变促使awk重新生成输出样式,这时新的 OFS 就派上用场了。
同时 改变 $0 的值也是一样,$0的值改变了,通常来讲都会影响到 field值的改变(只是我用于理解的一种方式,$0=$0就不会改变$0),所以awk就会重新生成每个field的值,这时 FS 便派上用场了。

It is a not-uncommon error to try to change the field separators in a record simply by setting FS and OFS, and then expecting a plain ‘print’ or ‘print $0’ to print the modified record.

But this does not work, since nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as ‘$1 = $1’, as described earlier.

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
9 [报告]
发表于 2012-04-21 09:36 |只看该作者
英语盲学linux 发表于 2012-04-20 13:55
回复 2# waker


如果不是特殊需要,一般在BEGIN{}中设置FS/OFS就不会有诸多烦恼了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP