简体   繁体   English

SED在末尾添加新行

[英]SED adds new line at the end

If I execute this on Mac OS X 10.7.2 in the Terminal App: 如果我在终端应用程序的Mac OS X 10.7.2上执行此操作:

$ cat test.txt | od -c

I get 我懂了

0000000    t   e   s   t   1  \n   t   e   s   t   2                    
0000013

That's the real content of my test.txt file. 那就是我的test.txt文件的真实内容。 Ok. 好。

But if I execute sed it adds a new line of each file. 但是,如果我执行sed,它将在每个文件中添加新行。

$ sed 's/e/X/g' test.txt | od -c
0000000    t   X   s   t   1  \n   t   X   s   t   2  \n                
0000014

I don't want this new line. 我不要这个新线。 How can I fix this? 我怎样才能解决这个问题?

Use printf in awk. 在awk中使用printf Special case the first line so that it is printed without a newline. 第一行是特殊情况,以便在不换行的情况下进行打印。 All remaining lines are printed with a preceding newline. 其余所有行都打印有一个换行符。 This ensures that you never end with a newline. 这样可以确保您永远不会以换行符结尾。

Basic structure: 基本结构:

awk 'NR==1 { printf("%s", $0); next }
     { printf("\n%s", $0) }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM