gawk FS将记录分成单个字符
如果字段分隔符是空字符串,则每个字符将成为单独的字段
$echo hello | awk -F '' -v OFS=,'{$1 = NF OFS $1} 1' 5,h,e,l,o 但是,如果FS是可能匹配零次的正则表达式,则不会发生相同的行为: $echo hello | awk -F ' *' -v OFS=,'{$1 = NF OFS $1} 1' 1,hello 谁知道为什么会这样?我在gawk manual中找不到任何东西.FS =“”只是一个特例吗? 我最感兴趣的是理解为什么第二种情况不会将记录分成更多的字段.好像awk正在处理FS =“*”,如FS =“” 解决方法
有趣的问题!
我刚刚提取了gnu-awk 4.1.0的代码,我想我们可以在文件field.c中找到答案. line 371: * re_parse_field --- parse fields using a regexp. * * This is called both from get_field() and from do_split() * via (*parse_field)(). This variation is for when FS is a regular * expression -- either user-defined or because RS=="" and FS==" " */ static long re_parse_field(lo... 这一行:(第425行): if (REEND(rp,scan) == RESTART(rp,scan)) { /* null match */ 这里是您的问题中< space> *匹配的情况.实现没有增加nf,也就是说,它认为整行是一个单独的字段.注意这个函数也用在do_split()函数中. 首先,如果FS为空字符串,则gawk将每个字符分隔为其自己的字段. gawk的doc清楚地写了这个,也在代码中,我们可以看到: line 613: * null_parse_field --- each character is a separate field * * This is called both from get_field() and from do_split() * via (*parse_field)(). This variation is for when FS is the null string. */ static long null_parse_field(long up_to, 如果FS有单个字符,awk不会将其视为正则表达式.这也在doc中提到过.也在代码中: #line 667 * sc_parse_field --- single character field separator * * This is called both from get_field() and from do_split() * via (*parse_field)(). This variation is for when FS is a single character * other than space. */ static long sc_parse_field(l 如果我们读取函数,那里就没有进行正则表达式匹配处理. 在函数re_parse_field()和sc_parse_field()的注释中,我们看到do_split也会调用它们.它解释了为什么我们在以下命令中有1而不是3: kent$ echo "foo"|awk '{split($0,a,/ */);print length(a)}' 1 注意,为了避免使帖子过长,我没有在这里粘贴完整的代码,我们可以在这里找到代码: http://git.savannah.gnu.org/cgit/gawk.git/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- linux – GNU malloc_info():获得真正分配的内
- linux – 多产PL2303串口到250000bps
- 解决ERROR: No toolchains found in the NDK too
- 【微信开发】cURL error 60: SSL certificate pr
- linux – 在ps -aux中CPU%超过100
- linux – 如何允许wget覆盖文件
- linux – 用于部署和保持ubuntu计算机最新的可能
- linux – Mono(2.11)上F#的尾调用优化的当前状态
- linux – 在终端窗口启动文件管理器wheezy
- linux – 如何将多个ffmpeg流组合成一个4×4拼贴