简体   繁体   English

如何从所有行中删除最后一个字符,但前提是该字符是某个特定字符

[英]How to remove the last character from all lines but only if it is a certain character

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 提前致谢

You can use sed for this: 您可以为此使用sed

$ 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. 请注意,如果要进行就地编辑,可以使用sed -i

awk版本:

awk '{sub(/\|$/,"")}1' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM