about abs guide

本文探讨了如何利用sed命令去除管道中输入文本的换行符问题,通过对比不同方法,如使用tr命令的效果,并指出sed在处理文本尾部换行符时存在的局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

////////////////////////////////////////////////////////////////

I tried to remove all newlines in a pipe like this:

(echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g' | hexdump -C

Which results on debian squeeze in:

00000000  66 6f 6f 20 62 61 72 0a                           |foo bar.|
00000008

Not removing the trailing newline.

tr -d '\n' as in How do I remove newlines from a text file? works just fine but isn't sed.


 

Sorry can't be done using sed, please see:http://sed.sourceforge.net/sedfaq5.html#s5.10and a discussion here: http://objectmix.com/awk/26812-sed-remove-last-new-line-2.html

Looks like sed will add back the \n if it is present as the last character.

/////////////////////////////////////////////////////////////////////////////////
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值