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

user2432819 picture user2432819 · Jan 7, 2014 · Viewed 26.7k times · Source

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.

Example:

~~~{ZRGHS|

~~~{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.

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?

Thanks!

Answer

Lev Levitsky picture Lev Levitsky · Jan 8, 2014

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 n occurrences:

    $ 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/(.*)([^ ]+) ([^ ]+)$/\1\n\2 \3/;h;s/\n.*//;s/ //g;G;s/\n.*\n//'
    onetwothree four
    

    with a file:

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