28

I have a file with about 1000 lines. All lines begin with a seven-digit number except for the occasional line. I need to catch these lines and actually join them with the previous line.

I've managed to be able to match any line that begins with a seven-digit number by using the following regex pattern:

^\d\{7}

I can't seem to get it to match any line that does not match this pattern though, which is really what I'm after.

As a second question that I'll embed into this one. Is it possible to have any lines that match (or do not match to stay consistent with what I'm trying to do) to join themselves to the previous line (as opposed to the J command that brings the next line up to the current one)?

Thanks

4 Answers 4

38
^\(\d\{7}\)\@!

This is vim's regex syntax for a negative lookahead.

If you're doing this as a mass : command, you should be able to just do

:v/^\d\{7}/-1j
9
  • Oddly enough, they are both resulting in the same thing. Though to me it doesn't make sense that the one without the ^ is actually working, no that I think about it.
    – Jason Down
    Sep 21, 2009 at 19:09
  • The only difference is that the one without the ^ will match if there is a seven-digit number somewhere else on the line other than the beginning.
    – chaos
    Sep 21, 2009 at 19:14
  • Ah, yes I see. In this case there wasn't, so no extra lines were modified... explains why the count for fewer lines was exactly the same.
    – Jason Down
    Sep 21, 2009 at 19:28
  • The :v/^\d\{7}/-1j command does not work for me: some of the "good" lines were joined with "bad" lines; and the joined lines has a space in between.
    – Hai Vu
    Sep 21, 2009 at 23:50
  • @Hai Vu: I think you're misunderstanding what it's doing. It's joining the previous line to the seven-digit-number line, regardless of what that previous line is. The space between is the standard behavior of the j (join) command, though that can be tweaked.
    – chaos
    Sep 22, 2009 at 1:47
9

Now, for the real answer

The regular expression which matches strings that do not begin with 7 digits is very simple:

.{0,6}([^0-9].*)?

A more classic regex equivalent without {} syntax is actually more readable: it visually shows us what is going on:

(|.|..|...|....|.....|......)([^0-9].*)?

I.e. match between 0 and 6 characters which can be anything, optionally followed by a nondigit that, if it occurs, may be followed by zero or more additional characters. This ensures that if a string seven characters characters or longer is matched, at least one of the first seven characters is a nondigit.

To translate this to matching lines in Vim, we add some escaping and anchoring:

^.\{0,6\}\([^0-9].*\)\?$

I don't "do" \d; it's too new-fashioned. :)

1
8

The regexp

^\(\d\{7}\)\@!

will invert your original expression. For more info on this syntax, see here:

:help \@!

As for the second problem, you could for example replace the trailing newline on the previous lines with nothing:

:%s/$\n^\(\d\{7}\)\@!//g
3
  • This one was close, but the join from the other answer left a space between the lines that were joined together (which is actually what I need). This example puts the last character from the first line directly before the first character from the next line. This is a side effect I need to avoid. Still, +1 for your answer and thank you.
    – Jason Down
    Sep 21, 2009 at 18:52
  • Oh well! The space can be added like this though: :%s/$\n^\(\d\{7}\)\@!/ /g
    – tobiasvl
    Sep 21, 2009 at 18:58
  • True enough. The other one is shorter though and I'm lazy ;P
    – Jason Down
    Sep 21, 2009 at 19:11
1

Here is my solution using awk, which can be called inside vim:

$ cat seven.awk
# Script to join lines that does not start with 7 digits
/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ { print; next }
{ printf $0; getline; print }

$ cat seven.txt
123
4579 bad
7654321 This line is OK
1234567 OK So is this
111
2222 bad again
4443333 OK again

$ awk -f seven.awk seven.txt
1234579 bad
7654321 This line is OK
1234567 OK So is this
1112222 bad again
4443333 OK again

Please excuse my clumsy expression: my awk is old and it does not understand fancy expression like: \d{7}. If you want to invoke this command from inside vim for the entire file:

:%!awk -f seven.awk
1
  • I originally had [0-9] seven times as well. I was quite pleased that the ex editor in gVim allowed the \d{7} shortcut.
    – Jason Down
    Sep 22, 2009 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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