awk CR LF处理是否在cygwin上中断? [英] Does awk CR LF handling break on cygwin?

查看:81
本文介绍了awk CR LF处理是否在cygwin上中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上,它可以按预期运行:

On Linux, this runs as expected:

$ echo -e "line1\r\nline2"|awk -v RS="\r\n" '/^line/ {print "awk: "$0}'
awk: line1
awk: line2

但是在Windows中,\ r被删除了(awk认为这一行):

But under windows the \r is dropped (awk considers this one line):

Windows:

$ echo -e "line1\r\nline2"|awk -v RS="\r\n" '/^line/ {print "awk: "$0}'
awk: line1
line2

Windows GNU Awk 4.0.1 Linux GNU Awk 3.1.8

Windows GNU Awk 4.0.1 Linux GNU Awk 3.1.8

@EdMorton的编辑(很抱歉,如果这是不必要的添加,但我认为这可能有助于说明问题):

EDIT from @EdMorton (sorry if this is an unwanted addition but I think maybe it helps demonstrate the issue):

考虑此RS设置和输入(在cygwin上):

Consider this RS setting and input (on cygwin):

$ awk 'BEGIN{printf "\"%s\"\n", RS}' | cat -v
"
"
$ echo -e "line1\r\nline2" | cat -v
line1^M
line2

这是带有gawk的Solaris:

This is Solaris with gawk:

$ echo -e "line1\r\nline2" | awk '1' | cat -v   
line1^M
line2

这是与gawk合作的cygwin:

and this is cygwin with gawk:

$ echo -e "line1\r\nline2" | awk '1' | cat -v
line1
line2

RS只是它的默认换行符,所以control-M在cygwin中去了哪里?

RS was just it's default newline so where did the control-M go in cygwin?

推荐答案

我刚刚与Arnold Robbins(gawk的提供者)进行了核对,答案是C库完成了此操作,如果要阻止它发生,您应该设置将awk BINMODE变量设置为3:

I just checked with Arnold Robbins (the provider of gawk) and the answer is that it's something done by the C libraries and to stop it happening you should set the awk BINMODE variable to 3:

$ echo -e "line1\r\nline2" | awk '1' | cat -v
line1
line2

$ echo -e "line1\r\nline2" | awk -v BINMODE=3 '1' | cat -v
line1^M
line2

如果有兴趣,请参见手册页以获取更多信息.

See the man page for more info if interested.

这篇关于awk CR LF处理是否在cygwin上中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆