1

I have a file containing integers (line numbers) and text. By extracting a PDF-file those line numbers got put into seperate lines from the text and I want to delete all those line numbers. I'm not very skilled with regexes but I'm 100% sure that it shouldn't be super hard. I use vim/nvim, but if I had to use awk/sed that would work for me, too.

File looks something like this:

123
124
125

Text was found in
those three lines
should stay

So ideally I want to search for: all lines, that start with a positive integer (1,2,3...123456) and contain nothing but that integer. They should be deleted.

Any help is much appreciated.

Bonus challenge: for some reason, some lines start with ^L1234, so there's an additional special character. At some point I'd want to delete these as well. Can be separate steps though.

0
  • [0-9] is a simple collection of digits that works almost anywhere. In Vim, you can also use \d (decimal digit), too.
  • You want one or more instances: append the \+ multiplier.
  • That should be the only stuff in the line, so the regexp needs to be anchored to the beginning and end (via ^ and $, respectively).
  • You want to attempt a match on all lines; in Vim, that's done via :global/{pattern}/.
  • Matching lines should be :deleted.

Taken together:

:global/^[0-9]\+$/delete

You can also use sed; it's faster and works non-interactively. As this automatically applies any rule to all input lines, you don't need the :global, and the :delete is just a d there. With -i, the replacement is done in place (so be sure to have it right and have a backup copy of the original file!)

sed -i -e '/^[0-9]\+$/d' file

For the optional prefix, the multiplier for that is \?. If that ^L is a single character, you probably need to enter it in Vim as <C-V><C-L>.

  • Thank you so much. I actually was closer than I thought. Escaping the + character helped a lot, and from there on, I might have guessed the missing $ sign myself. The <C-V><C-L> thing worked just as fine. – Paul Feb 8 '18 at 11:39
  • As you can see, there are subtle differences between regexp dialects, not just between Vim and sed. In Vim, you can even influence the amount of escaping (via \V / \v). You don't need to guess with Vim; it has excellent help. :help patterns would have been a good start here. – Ingo Karkat Feb 8 '18 at 12:02
0

Remove lines starting and containing only integers in Vim

:%s/^[0-9]\+\n//gc

Explanation

  • :%s// - global search and replace
  • ^ - start at beginning of line
  • [0-9] - match digits
  • \+ - greedy match one or more
  • \n match newline (Note: check your line endings, these vary between systems and may be eg \r\n instead)
  • gc - global match + confirm (remove c if there's lots or you're sure

Output

123
124
125

Text was found in
those three lines
should stay

:%s/^[0-9]*\n//g

Text was found in
those three lines
should stay

Bonus

Per terdon's answer on U&L, the ^L is the form feed character, wih can be removed using sed:

sed 's/\o14//g' file 

It is worth reading that answer to see other ways of doing so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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