AWK字符串连接没有工作2(壳设置错误?) [英] awk string concatenation not working 2 (shell setting error?)

查看:107
本文介绍了AWK字符串连接没有工作2(壳设置错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种奇怪的现象,我不明白。我想我失去了一些东西在AWK重要。我想累积串联整条生产线,以一个名为C字符串时存在包含信号的线路。该字符串以'a'和'B'的串联工作正常。

I have this strange phenomenon I don't understand. I guess I'm missing something important in awk. I want to cumulatively concatenate the whole line to a string called 'c' when there is a line containing SIGNAL. The string starts with a concatenation of 'a' and 'b' which works fine.

文件 in.dat

SIGNAL Hello1!

文件 tt.awk

BEGIN    { a = "a"; b = "b"; c = a b; }
/SIGNAL/ { c = c " " $0; }
END      { print c; }

当我这样做的awk -f tt.awk in.dat 我得到(预期):

ab SIGNAL Hello1!

现在我换 in.dat 来:

SIGNAL Hello1!
SIGNAL Hello2!

然后我的awk -f tt.awk in.dat 再次获得:

 SIGNAL Hello2!1!

我期望看到:

ab SIGNAL Hello1! SIGNAL Hello2!

我这样做对我的CentOS的外壳(在我的〜/ .cshrc中文件一堆设置)。我检查了这些在我的Cygwin的外壳和使用正常,如我所料。什么是错我的CentOS的贝设置。难道是什么?

I am doing it on my CentOS shell (with bunch of settings in my ~/.cshrc file). I checked these on my Cygwin shell and it works normally as I expect. Something is wrong with my CentOS shell setting. What could it be?

推荐答案

这是用DOS行结束的问题(在评论上述由伊坦赖斯纳已注明)。你的的第二个版本in.dat 使用 \\ r \\ n 换行和 AWK 无法面对这一切。

This is a problem with DOS line endings (as noted by Etan Reisner in the comments above). Your second version of in.dat uses \r\n for line breaks and awk can't deal with that.

用你相同的 tt.awk code:

Using your same tt.awk code:

$ echo "SIGNAL Hello1\!\nSIGNAL Hello2\!" |awk -f tt.awk
ab SIGNAL Hello1! SIGNAL Hello2!
$ echo "SIGNAL Hello1\!\r\nSIGNAL Hello2\!" |awk -f tt.awk
 SIGNAL Hello2!1!

想知道这是真的在做什么?在UNIX中, \\ r 将重置行最左边的地方的位置,但确实的的送你下了线(这是什么的 \\ n 一样)。 DOS间$ P $点 \\ n 为下降线,但不复位到最左边的位置,而UNIX取 \\ r 为隐式。

Wondering what this is really doing? In UNIX, \r resets the position in line to the leftmost place but does not send you down a line (that's what \n does). DOS interprets \n as going down a line but not resetting to the leftmost position while UNIX takes the \r as implicit.

下面是一些实验来说明这是怎么回事:

Here are some experiments to illustrate what's going on:

$ echo "SIGNAL Hello1\!\r\nSIGNAL Hello2\!"
SIGNAL Hello1!
SIGNAL Hello2!
$ echo "SIGNAL Hello1\!\rSIGNAL Hello2\!"
SIGNAL Hello2!
$ echo "ab SIGNAL Hello1\!\n SIGNAL Hello2\!"
ab SIGNAL Hello1!
 SIGNAL Hello2!
$ echo "ab SIGNAL Hello1\!\r SIGNAL Hello2\!"
 SIGNAL Hello2!1!

要特别注意最后两个项目。 AWK 剥离 \\ n 你,但保留了 \\ r ,所以第一个行打印为 AB信号Hello1!,然后 \\ r 被应用,而第二行信号Hello2!写的上的最常见的第一道防线。第一行的最后两个字符( 1 <!/ code>)仍因为第二线不够长,覆盖它们。

Pay special attention to the last two items. awk strips the \n for you, but retains the \r, so the first line prints as ab SIGNAL Hello1! and then the \r is applied, and the second line signal Hello2! is written on top of that first line. The first line's final two characters (1!) remain because the second line wasn't long enough to overwrite them.

现在我们知道了问题,我们可以修正code:

Now that we know the issue, we can fix the code:

BEGIN    { a = "a"; b = "b"; c = a b; }
/SIGNAL/ { gsub(/\r/, ""); c = c " " $0; }
END      { print c; }

这将删除所有 \\ r 期从被添加到 C 行。

This removes all \rs from lines that are added to c.

这篇关于AWK字符串连接没有工作2(壳设置错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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