如何使用shell脚本删除文本文件中最后一个换行符

2025-04-15 11:47:57
推荐回答(1个)
回答(1):

[root@localhost wwe]# cat -E good
$
good$
[root@localhost wwe]#
1.借助tr命令
[root@localhost wwe]# tail -n1 good | tr -d '\n' >> oo
[root@localhost wwe]# cat -E oo
good[root[/color]@localhost wwe]#
2.将good中的最后一行删除
[root@localhost wwe]# sed -i '$d' good
[root@localhost wwe]# cat -E good
$
3.将oo中的处理了换行字符的结果写入good
[root@localhost wwe]# sed -i 'r oo' good
[root@localhost wwe]# cat -E good
$
good[root@localhost wwe]#