LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 835|回复: 2

awk 中 \n 的意思

[复制链接]
发表于 2010-8-17 16:35:31 | 显示全部楼层 |阅读模式
[root@shpdl380prx2 nm308]# cat data2
Riley Mullen
123 Main Street
Chicago, IT 60601
(312)555-1234

Frank Williams
456 Oak Street
Indianapolis, IN 46201
(317)555-9876

Haley Snell
4231 Elm Street
Detroit, MI 48201
(313)555-4938
[root@shpdl380prx2 nm308]# awk 'BEGIN{FS="\n";RS=""} {printf "%-16s %s\n", $1, $4}' data2

这里为什么要写成 FS="\n" RS=""

不知道有哪位高人知道
发表于 2010-8-17 19:19:40 | 显示全部楼层
RS 为空字符串表明以空行为 records 分隔符,换行为 fields 分隔符
回复 支持 反对

使用道具 举报

发表于 2010-8-17 22:25:21 | 显示全部楼层

好像原书中的答案!?

If you try to read this data using the default FS and RS variable values, gawk will read each line
as a separate record, and interpret each space in the record as a field separator. This isn’t what
you intended.
To solve this problem, you need to set the FS variable to the newline character. This indicates
that each line in the data stream is a separate field and all of the data on a line belongs to the
data field. However, now you have the problem of not knowing where a new record starts.
To solve this problem, set the RS variable to an empty string, then leave a blank line between data
records in the data stream. The gawk program will interpret each blank line as a record separator.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表