开发者

Shell script: Removing leading and trailing spaces between fields in a pipe delimited file

开发者 https://www.devze.com 2023-03-23 13:22 出处:网络
I have a pipe delimited file as below 1|Mike|2000|2| 2|Peter|4000|2| ..... ... .... and so on. I want to remove the leading and trailing spaces between fields. It should look like as below

I have a pipe delimited file as below

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

..... ... .... and so on.

I want to remove the leading and trailing spaces between fields. It should look like as below

1|开发者_JS百科Mike|2000|2|
2|Peter|4000|2|

Is there any way in shell script to achieve this output?

Thanks, Chandraa


You can try

cat datafile | tr -d ' \t'

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


Try this:

cat datafile | sed -e 's/[ \t]*|[ \t]*/|/g'
0

精彩评论

暂无评论...
验证码 换一张
取 消