Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 281
user2432819 Avatar asked Jan 07 '14 22:01

user2432819


People also ask

How do you use sed to replace the first occurrence in a file?

With GNU sed's -z option you could process the whole file as if it was only one line. That way a s/…/…/ would only replace the first match in the whole file. Remember: s/…/…/ only replaces the first match in each line, but with the -z option sed treats the whole file as a single line.

How do you use sed command to replace a string in a file?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

Which sed command is used for replacement?

Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. Output : linux is great os.


2 Answers

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
    
like image 176
Lev Levitsky Avatar answered Oct 09 '22 00:10

Lev Levitsky


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.

like image 42
Sir Athos Avatar answered Oct 09 '22 00:10

Sir Athos