How to remove the last character from all lines bu

2019-09-09 03:39发布

i have data in this form

Kure|Hiroshima
Kure|Hiroshima
Kure|Hiroshima
NO MATCHING
Aachen|
Darmstadt
Mulheim
Aachen|

and i want to remove the last character only if it is a pipe (|).

desired output

Kure|Hiroshima
Kure|Hiroshima
Kure|Hiroshima
NO MATCHING
Aachen
Darmstadt
Mulheim
Aachen

Thanks in advance

标签: linux string sed
2条回答
何必那么认真
2楼-- · 2019-09-09 04:14

You can use sed for this:

$ sed 's/|$//' file
Kure|Hiroshima
Kure|Hiroshima
Kure|Hiroshima
NO MATCHING
Aachen
Darmstadt
Mulheim
Aachen

It looks for | together with $ (which means "end of line") and deletes it.

Note you can use sed -i in case you want to do an in-place edit.

查看更多
地球回转人心会变
3楼-- · 2019-09-09 04:41

An awk version:

awk '{sub(/\|$/,"")}1' file
查看更多
登录 后发表回答