最後の\ nを削除する方法は?

最後の\ nを削除する方法は?

PythonでCLIコマンドを使用して、ps -au | awk '{print$1,$2,$3,$4,$7,$11}' | sed '1d'次の出力を取得します。

root 669 0.0 0.0 tty1 /sbin/agetty
root 670 0.4 2.8 tty7 /usr/lib/xorg/Xorg
lothar 1573 0.0 0.0 pts/0 sh
lothar 1599 0.0 0.0 pts/0 /bin/bash
lothar 21130 0.0 0.1 pts/1 /usr/bin/bash
lothar 21140 0.0 0.1 pts/2 /usr/bin/bash
lothar 21146 0.0 0.1 pts/3 /usr/bin/bash
lothar 21230 0.0 0.1 pts/4 /usr/bin/bash
lothar 508386 0.0 0.0 pts/0 ps
lothar 508387 0.0 0.0 pts/0 [awk]
lothar 508388 0.0 0.0 pts/0 sed

ただし、このコマンドセットは追加の\n文字を挿入します。 Linux cliでtrを使用してすべて削除できることを知っていますが、\n最後の項目だけを削除するだけです。

これを行う最良の方法は何ですか?

ありがとうございます!

ベストアンサー1

これらの問題を解決する簡単なツールがあります。

truncate -s -1

しかし、最初に末尾の改行があることを確認することをお勧めします。

[ -z "$(tail -c1 file)" ] && truncate -s -1

おすすめ記事