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

3

6

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!

Ichnite answered 7/1, 2014 at 22:50 Comment(1)
sed would do the job, but many tools can do it. Do it with the one you are most comfortable with.Haematoid
H
8

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
    
Heeling answered 7/1, 2014 at 23:1 Comment(5)
Yes, that is what I want to do, but for each row, so this would need to be repeated multiple times, where each row is separated by \nIchnite
@Ichnite That's how sed works, just give it the file name and it applies the command to each line.Heeling
okay, so if I wanted to use this command on a file, would sed -i | rev | sed | 's/ //2g' | rev file.txt work?Ichnite
Thank you so much! The last examples looks very complicated, but I will at least try the example with a file out, and then learn more about each part of the command. I have very little to no experience with sed, and only know the very basics.Ichnite
@Ichnite The third one is for my own entertainment, but the other two are more understandable. Feel free to ask questions here in the comments, and to accept the answer if you find it appropriate.Heeling
P
6

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.

Phonetics answered 7/1, 2014 at 22:58 Comment(1)
I am loading the data using a copy command, and specifying a parser or delimiter which is a space, ' '. And it worked, except for the rejected rows, which were all placed into a file.Ichnite
P
0

To strip/remove 1st character from string:

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

Similar to remove traling character:

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

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

Peabody answered 17/11, 2017 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.