使用 shell 脚本从管道分隔文件中的字段中删除空格
我是 UNIX Shell 脚本编写的新手。
我需要帮助从字段中删除前导和尾随空格。但我需要保留单词之间的空格。
请查看下面的数据示例和所需的结果以了解我的问题。
数据样本:
1-B48980007 |82984788|317 |ALQ| |4423271 | 0| |
I0000000000000000000245729|28887957|IL FR | | |00000000573| 0| |
I0000000000000000000245715|13822348|RPVIPPR | | |00000000298| 0| |
I0000000000000000000245721|15348717|AN BV | | |00000001526| 0| |
期望的结果:
1-B48980007|82984788|317|ALQ||4423271|0||
I0000000000000000000245729|28887957|IL FR|||00000000573|0||
I0000000000000000000245715|13822348|RPVIPPR|||00000000298|0||
I0000000000000000000245721|15348717|AN BV|||00000001526|0||
但是我使用以下命令得到如下输出:
sed 's/ *\|/\|/g' file_name > testOP
pipeline('|') 是我的文件中的分隔符。我需要删除管道前后的空格,但需要保留单词之间的空格,例如:“IL FR”和“AN BV”。
1-B48980007 |82984788|317|ALQ||4423271| 0||
I0000000000000000000245729|28887957|IL FR| ||00000000573| 0||
I0000000000000000000245715|13822348|RPVIPPR| ||00000000298| 0||
I0000000000000000000245721|15348717|AN BV| ||00000001526| 0||
非常感谢任何帮助。
谢谢, 萨维莎
I am new to UNIX Shell scripting.
I need help in removing leading and trailing blank spaces from the fields. But I need to retain the spaces between the words.
Please have a look at the data sample and the desired result below to understand my problem.
Data Sample :
1-B48980007 |82984788|317 |ALQ| |4423271 | 0| |
I0000000000000000000245729|28887957|IL FR | | |00000000573| 0| |
I0000000000000000000245715|13822348|RPVIPPR | | |00000000298| 0| |
I0000000000000000000245721|15348717|AN BV | | |00000001526| 0| |
Desired Result:
1-B48980007|82984788|317|ALQ||4423271|0||
I0000000000000000000245729|28887957|IL FR|||00000000573|0||
I0000000000000000000245715|13822348|RPVIPPR|||00000000298|0||
I0000000000000000000245721|15348717|AN BV|||00000001526|0||
But I am getting the output as below on using the below command:
sed 's/ *\|/\|/g' file_name > testOP
pipeline('|') is a delimiter in my file. I need to remove the spaces before and after the pipeline but need to retain the spaces between the words for example: "IL FR" and "AN BV".
1-B48980007 |82984788|317|ALQ||4423271| 0||
I0000000000000000000245729|28887957|IL FR| ||00000000573| 0||
I0000000000000000000245715|13822348|RPVIPPR| ||00000000298| 0||
I0000000000000000000245721|15348717|AN BV| ||00000001526| 0||
Any help is greatly appreciated.
Thanks,
Savitha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
给出所需的结果:
请注意,此方法仅删除空格字符。要排除所有空格,还必须考虑制表符。对于任何 POSIX 兼容的 sed 实现,您可以这样做:
或者,使用正则表达式的 GNU 扩展:
Using:
gives the desired result:
Note that this approach removes only space characters. To exclude all whitespace, tab characters must be accounted for as well. With any POSIX-compliant implementation of sed, you could do this:
Or, with GNU extensions to the regex:
这可能有效:
编辑:删除不必要的括号和交替
This might work:
EDIT: removed unnecessary parens and alternation
我用下面的 sed 语句解决了这个问题:
sed -e
's/ *\|/\|/g'
-e's/press_tab_key_here*\|press_tab_key_here*/\|/ g'
-e's/\| */\|/g'
file_name要删除制表符空格,我必须按“制表符”键。 '\t' 在我的情况下不起作用。
感谢 Michael、Potong 和 Triplee 的帮助和支持。 :)
I resolved the issue with the below sed statement:
sed -e
's/ *\|/\|/g'
-e's/press_tab_key_here*\|press_tab_key_here*/\|/g'
-e's/\| */\|/g'
file_nameto remove the tab spaces, I had to press "tab" key. '\t' didn't work in my case.
Thanks Michael, Potong and Triplee for all the help and support. :)
考虑将逗号作为 csv 或文本文件中数据列的分隔符,这是一种删除前导尾随的分隔符,使空格成为单一的。
considering comma as separator in csv or text files for data columns here is one to remove leading trailing, makes space single.