Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for a solution about a "paste" problem on vi/vim .

How to join specific lines in a text file? I have a file like this:

15/10/2011 Revolution day
16/10/2011 About Rome
 - differents newspapers
  blogs an feeds
 :) hp
17/10/2011 Yep, another day

and I want to paste everyline that does not start with a number. I've tried something like this... but does not work :-/

:[!^[0-9]]!paste -d ' ' -s

Can somebody help me?

thanks gb

share|improve this question
add comment

5 Answers 5

:%s/\(.*\)\n\(^[^0-9]\)/\1\2/g

If you want a space or something between, add it between \1 and \2.

share|improve this answer
    
ah, I was having problems doing it in a single command. thanks. :) –  Tim Kennedy Oct 28 '11 at 14:32
    
GREAT! Thank you –  gogolb Oct 28 '11 at 14:46
add comment

you can do it in two steps in VIM, if that's acceptable:

First, join all lines into a single line.

with the file open in VIM, issue the following keystroke: VGJ

  • V = Visual Mode (linewise, rather than character-wise (v))
  • G = highlight all lines
  • J = Join all lines

Second, split your lines on the date format the resulting lines start with.

with the file open in VIM, issue the following command:

:%s#\([0-9][0-9]/[0-9][0-9]/[0-9]\)#^M\1#g

This will search the single joined line for any string that matches NN/NN/N, where N = 0-9, which matches the date format that your lines start with, and will split on that match.

share|improve this answer
add comment

Use the v with join.

:v/^[0-9]/-1,join

Broken down:

  • :v select any lines that do not match regular expression
  • /^[0-9]/ any line starting with a digit
  • -1, the previous line to the current line
  • join combine the lines selected, separated by whitespace
share|improve this answer
add comment

I'm talking about vim, this will not run in vi. Be aware of that.

What about

    :%v/^[^0-9]/d

to delete all lines in buffer , which start with a number.

    ggVG

to highlight all lines

    :w ! paste -d ' ' -s

and after that

    u

to go back to the starting point

share|improve this answer
    
It does not seems to work. Morevorer, I have stated an incorrect question. I want to join every line not starting by a number to the previous line starting with a number. –  gogolb Oct 28 '11 at 13:34
add comment

please try:

:g/^\d/,/^\d/-1j

and ignore error message E16: Invalid range

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.