Keep spaces when using sed command in kornshell script

青春壹個敷衍的年華 提交于 2019-12-24 05:26:12

问题


I'm having some trouble with the sed command :

I'd like to replace in a string, a single quote by two single quote
Here's my string :

04CA07807800106109XE00187200000081960          NI         0780643442        178'0700

I used this sed command to replace the single quote :

line=`echo $line | sed "s/'/''/g"`

The result is

04CA07807800106109XE00187200000081960 NI 0780643442 178''0700

As you can see, the single quote is properly replaced by two single quotes.
But I lost all the spaces between each character of my string and I really need to keep them.

Is there a way to keep those spaces with the sed command ?
I tried with a tr command and i got the same result.

Thank you for your help.


回答1:


Problem is not sed command but your unquoted variable. Use:

line=`echo "$line" | sed "s/'/''/g"`

Unquoted variable causes word splitting which results in multiple parameters to echo, resulting in single spaced words.



来源:https://stackoverflow.com/questions/37306796/keep-spaces-when-using-sed-command-in-kornshell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!