0

Many of you with a certain leaning towards proper formatting will know the pain of having a lot of space characters insted of a tab character in the beginning of indented lines after another person edited a file and added lines. I seem to be unable to teach my colleagues how to use vim's integrated line pasting function, so I'm searching for some simple ways to automatically correct lines beginning with a certain pattern. ;)

I'm using a regex to find the corresponding lines, but I can't work out how to "reuse" the last matched character in sed when using "find and replace". The regex matching the lines is

'^\ *[A-Z]'

I would like to replace those space characters, but keep the uppercase letter. My idea would be something like

sed 's|^\ *[A-Z]|\t$|g'

or so, but I guess that would replace the whole line with a single tab character since $ usually matches the line ending?

Is there a simple way to reuse parts of the matched regex in sed?

David
  • 308
  • 1
  • 12

1 Answers1

3

How about simply not including the first non-space character in the match in the first place?

This matches all spaces at the beginning of a line:

^ *

Edit (quote from the comments):

obviously I don't want to replace spaces in front of other characters than uppercase letters

A look-ahead could do that, but unfortunatey sed does not support them. But you can use the next best thing, an expression that determines which lines sed operates on:

sed '|^ *[A-Z]| s|^ *|\t|'

Of course a back-reference would do it as well:

sed 's|^ *\([A-Z]\)|\t\1|'
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    I know that, but obviously I don't want to replace spaces in front of other characters than uppercase letters. Thanks though.^^ – David Oct 16 '15 at 09:40
  • 1
    @David: No problem. sed can use preconditions: http://stackoverflow.com/a/12178023/18771, just use `/^ *[A-Z]/` as your's. – Tomalak Oct 16 '15 at 09:43
  • 1
    That's what I call "escaping voodoo" - escaping characters "just to be sure". The space is not a special character in regular expressions, there is no need to escape it, unless the specific environment (e.g. shell) requires it for other reasons. – Tomalak Oct 16 '15 at 09:46