在kornshell脚本中使用sed命令时保留空格 [英] Keep spaces when using sed command in kornshell script

查看:121
本文介绍了在kornshell脚本中使用sed命令时保留空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用sed命令时遇到了一些麻烦:

我想用两个单引号替换一个字符串,一个单引号
这是我的琴弦:

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

我使用此sed命令替换单引号:

I used this sed command to replace the single quote :

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

结果是

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.

有没有一种方法可以使用sed命令保留这些空间?
我尝试使用tr命令获得了相同的结果.

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

谢谢您的帮助.

推荐答案

问题不是sed命令,而是您未引用的变量.使用:

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

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

未加引号的变量会导致分词,从而导致多个参数进行 echo ,产生单个空格的单词.

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

这篇关于在kornshell脚本中使用sed命令时保留空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆