Search code examples
linuxunixsedvi

Using sed or VIM to replace space with new line


I have the following data.

1455931_at Chrna3 1420468_at Asb17 1445520_at −−− 1436717_x_at Hbb−y 1431788_at Fabp12 1458975_at −−−

With sed or VIM editor how can I change it to

1455931_at Chrna3 
1420468_at Asb17 
1445520_at −−− 
1436717_x_at Hbb−y 
1431788_at Fabp12 
1458975_at −−−

So all the word that has _at will be the first of every line. Every line consist of pairwise _at and gene terms.


Solution

  • In Vim, I would do this:

    :%s/ /^M/g
    :g/_at/j
    

    Where the ^M is typed by pressing control-V (control-Q on Windows) followed by the Enter/Return key.

    This assumes single spaces between tokens; as @Floris suggests, you can use s/ \+/^M/g to turn multiple consecutive spaces into a single newline. Or you could use s/\v\s+/^M/g to do the same thing with any consecutive whitespace including tabs as well as literal space characters.