awk - 无法指定 FS
我正在使用 awk 处理 nroff 格式的联机帮助页,以提取每个命令的选项...我发现选项以 \fB
开头,后面是实际选项,也许还有 \fP
和选项参数等等...
示例:
\fB\-\-author\fR
我开始编写一个 awk 脚本,指定 FS = "\fB"
...好吧,它不起作用。 .. 我尝试转义 \
,切换到 FS = "\\fB"
但也没有工作...我做错了什么?
这是我的脚本:
BEGIN {
FS = "\\f." # "\\\\f." didn't work either
}
{
print $2
}
这是输入
\fB-o\fP
我希望 $2 为 -o。但这是行不通的。
I'm processing a manpage in nroff format with awk to extract the options to each command... I figured out that the options start with \fB
, followed by the actual option, and maybe \fP
and option arguments and so on...
Example:
\fB\-\-author\fR
I started writing an awk-script, specifing FS = "\fB"
... well, it didn't work... I tried to escape the \
, switching to FS = "\\fB"
but didn't work either... what am I doing wrong?
This is my script:
BEGIN {
FS = "\\f." # "\\\\f." didn't work either
}
{
print $2
}
This is the input
\fB-o\fP
Where I want $2 to be -o. But it just won't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来你可以用 4 个反斜杠来完成这个:
当 bash 解析这个时,它应该将 4 个反斜杠转义为 2 个文字反斜杠;然后 awk 会将那些 2 个反斜杠转义为单个反斜杠。
It looks like you can accomplish this with 4 backslashes:
When bash parses this, it should unescape the 4 backslashes to 2 literal backslashes; then awk will unescape those 2 backslashes to a single literal backslash.
字段分隔符
FS
用于类似 CSV 的数据。根据您的情况,找到过滤器的选项,然后删除您不需要的部分:The field separator
FS
is for CSV-like data. In your case, find the options for a filter and then remove the parts that you don't want:我想我记得曾经遇到过这个。
真正的问题是 awk 的某些版本坚持 FS 是单个字符。
我记得,解决方法是手动将文件拉入 GNU Emacs,将多字符 FS 编辑为文件中其他任何地方未使用的一个字符,使用适当的 FS 执行 awk,然后手动修复它。
您也许可以使用几个 sed 脚本来自动执行此操作,一个用于进行初始重新编码,一个用于修复它,中间使用 awk 步骤。
I think I remember running into this once.
The real problem was that some versions of awk insist on FS being a single character.
The way around it, as I recall, was to manually pull the file into GNU Emacs, edit the multicharacter FS down to one character that wasn't used anywhere else in the file, awk that with the appropriate FS, then manually repair it afterwards.
You MIGHT be able to automate this with a couple of sed scripts, one to do the initial recoding, and one to repair it, with the awk step in the middle.