Linux 如何使用sed用空字符串替换第一个空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20983631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:48:13  来源:igfitidea点击:

How to use sed to replace the first space with an empty string

regexlinuxbashsedtr

提问by user2432819

I am having trouble loading a space delimited text file into a table. The data in this text file is generated by teragen, and hence, is just dummy data, where there are only 2 columns, and the first column has values of random special character strings.

我无法将空格分隔的文本文件加载到表格中。这个文本文件中的数据是由teragen生成的,因此只是虚拟数据,只有2列,第一列是随机特殊字符串的值。

Example:

例子:

~~~{ZRGHS|

~~~{ZRGHS|

~~~{qahVN)

~~~{qahVN)

I run into a problem and get rejected rows because some of these values have a space in them as a random ASCII character, which causes it to think that there are 3 columns, when my table has 2, so they get rejected.

我遇到了一个问题并得到拒绝的行,因为其中一些值作为随机 ASCII 字符在其中有一个空格,这导致它认为有 3 列,当我的表有 2 列时,因此它们被拒绝。

So, what I want to do is remove only the first space from these rejected rows, which will need to be repeated multiple times over each row, and then try to reload them. Would sed be the best way to go about this, or would something else like tr be more appropriate?

所以,我想要做的是只从这些被拒绝的行中删除第一个空格,这需要在每一行上重复多次,然后尝试重新加载它们。sed 是解决此问题的最佳方法,还是 tr 之类的其他方法更合适?

Thanks!

谢谢!

回答by Sir Athos

To remove the first space from a line, use

要从一行中删除第一个空格,请使用

echo "my line with spaces" | sed 's/ //'

Depending on the specifics of your approach (fixed column length? how are you adding the data?) there might be a better way to do this in a single step instead of parsing rejected rows over and over.

根据您的方法的具体情况(固定列长度?您如何添加数据?)可能有更好的方法可以在一个步骤中完成此操作,而不是一遍又一遍地解析被拒绝的行。

回答by Lev Levitsky

From what I understand, you want to remove all spaces except the last two.

据我了解,您想删除除最后两个空格之外的所有空格。

  • You can build a regex for that, or you could use the fact that it's very easy to keep the first noccurrences:

    $ echo 'one two three four' | rev | sed 's/ //2g' | rev
    onetwothree four
    

    or, with a file:

    rev myfile | sed 's/ //2g' | rev
    
  • Or you could remove one space until there is only one space left:

    $ echo 'one two three four' | sed ':a;/ .* /{s/ //;ba}'
    onetwothree four
    

    with a file:

    sed ':a;/ .* /{s/ //;ba}' myfile
    
  • Or, if you're in the mood, you can split the line, play with it, and assemble it back (GNU sed assumed):

     $ echo 'one two three four' | sed -r 's/(.*)([^ ]+) ([^ ]+)$/\n /;h;s/\n.*//;s/ //g;G;s/\n.*\n//'
    onetwothree four
    

    with a file:

    sed -r 's/(.*)([^ ]+) ([^ ]+)$/\n /;h;s/\n.*//;s/ //g;G;s/\n.*\n//' myfile
    
  • 您可以为此构建一个正则表达式,或者您可以使用这样一个事实,即保留前 n次出现非常容易:

    $ echo 'one two three four' | rev | sed 's/ //2g' | rev
    onetwothree four
    

    或者,使用文件:

    rev myfile | sed 's/ //2g' | rev
    
  • 或者您可以删除一个空格,直到只剩下一个空格:

    $ echo 'one two three four' | sed ':a;/ .* /{s/ //;ba}'
    onetwothree four
    

    用一个文件:

    sed ':a;/ .* /{s/ //;ba}' myfile
    
  • 或者,如果你有心情,你可以拆分这条线,玩弄它,然后把它组装回去(假设是 GNU sed):

     $ echo 'one two three four' | sed -r 's/(.*)([^ ]+) ([^ ]+)$/\n /;h;s/\n.*//;s/ //g;G;s/\n.*\n//'
    onetwothree four
    

    用一个文件:

    sed -r 's/(.*)([^ ]+) ([^ ]+)$/\n /;h;s/\n.*//;s/ //g;G;s/\n.*\n//' myfile
    

回答by To Kra

To strip/remove 1st character from string:

从字符串中删除/删除第一个字符:

function stringStripStart { echo ${1:1:${#1}} }

function stringStripStart { echo ${1:1:${#1}} }

Similar to remove traling character:

类似于删除尾随字符:

function stringStripEnd { FINAL_LEN=${#1}-1 echo ${1:0:$FINAL_LEN} }

function stringStripEnd { FINAL_LEN=${#1}-1 echo ${1:0:$FINAL_LEN} }

Note:for empty string, some additional condition needs to be added.

注意:对于空字符串,需要添加一些附加条件。