Shell脚本:删除管道分隔文件中字段之间的前导和尾随空格

时间:2011-07-29 03:00:58

标签: shell

我有一个管道分隔文件,如下所示

1  |Mike    |   2000|     2|
2  |Peter   |   4000|     2|

..... ... ....等等。

我想删除字段之间的前导和尾随空格。它应该如下所示

1|Mike|2000|2|
2|Peter|4000|2|

shell脚本中是否有任何方法可以实现此输出?

谢谢, Chandraa

2 个答案:

答案 0 :(得分:5)

你可以尝试

cat datafile | tr -d ' \t'

cat datafile | tr -d '[:space:]' # will remove all spaces including the new line at the end of each line

答案 1 :(得分:2)

试试这个:

cat datafile | sed -e 's/[ \t]*|[ \t]*/|/g'
相关问题