使用AWK或SED删除字段的尾部和前导空格 [英] Removing trailing and leading spaces of a field using AWK or SED
本文介绍了使用AWK或SED删除字段的尾部和前导空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据库文本文件(类似csv),其中包含过多的尾随和前导空格.这些事件仅发生在用于分隔列的字符"|"周围.我的目标是使用awk或sed删除这些空间.我无法完成这项看似简单的任务;可能是因为|是一个特殊的角色?
I have a database text-file (csv-like) that contains excessive trailing and leading spaces. These incidents only occur around character "|", which is used to separate columns. My goal is to remove these spaces using awk or sed. I can't get this seemingly simple task to work; is it perhaps because | is a special character?
Input.txt
| |COL1 |COL2 |COL3 |COL4 |COL11|COL99|COL19 |COL88 |CAL9 |COL84 |COL98 |
| |500 |0001 |0100000000|1995 |001 | |Test This|00.00.0000| 6,14 | 12,00 | 0,00 |
| |500 |0001 |0100000000|1995 |002 | |Separ ated|00.00.0000| 18,14 | 12,00 | 0,00 |
Output.txt
||COL1|COL2|COL3|COL4|COL11|COL99|COL19|COL88|COL9|COL84|COL98|
||500|0001|0100000000|1995|001||Test This|00.00.0000|6,14|12,00|0,00|
||500|0001|0100000000|1995|001||Separ ated|00.00.0000|18,14|12,00|0,00|
推荐答案
这应该做到:
sed 's/ *| */|/g' file
||COL1|COL2|COL3|COL4|COL11|COL99|COL19|COL88|CAL9|COL84|COL98|
||500|0001|0100000000|1995|001||Test This|00.00.0000|6,14|12,00|0,00|
||500|0001|0100000000|1995|002||Separ ated|00.00.0000|18,14|12,00|0,00|
使用|
更改任何<space>|<space>
,因此不会删除其他空间.
It change any <space>|<space>
with |
so other space are not removed.
这篇关于使用AWK或SED删除字段的尾部和前导空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文