SED在末尾添加新行 [英] SED adds new line at the end

查看:3139
本文介绍了SED在末尾添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在终端应用程序的Mac OS X 10.7.2上执行此操作:

If I execute this on Mac OS X 10.7.2 in the Terminal App:

$ cat test.txt | od -c

我知道

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

那是我的test.txt文件的真实内容.好吧.

That's the real content of my test.txt file. Ok.

但是,如果我执行sed,它将在每个文件中添加新行.

But if I execute sed it adds a new line of each file.

$ 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?

推荐答案

在awk中使用printf.第一行是特殊情况,以便在打印时不使用换行符.其余所有行都打印有一个换行符.这样可以确保您永远不会以换行符结尾.

Use printf in awk. 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.

基本结构:

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

这篇关于SED在末尾添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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