2

I've got a bunch of files that have sentences ending like this: \@.Next sentence. I'd like to insert a space after the period.

Not all occurrences of \@. do not have a space, however, so my regex checks if the next character after the period is a capital letter.

Because I'm checking one character after the period, I can't just do a replace on \@. to \@., and because I don't know what character is following the period, I'm stuck.

My command currently:

sed -i .bak -E 's/\\@\.[A-Z]/<SOMETHING IN HERE>/g' *.tex

How can I grab the last letter of the matching string to use in the replacement regex?

EDIT: For the record, I'm using a BSD version of sed (I'm using OS X) - from my previous question regarding sed, apparently BSD sed (or at least, the Apple version) doesn't always play nice with GNU sed regular expressions.

Community
  • 1
  • 1
simont
  • 68,704
  • 18
  • 117
  • 136

4 Answers4

3

The right command should be this:

sed -i.bak -E "s/\\\@.(\S)/\\\@. \1/g" *.tex

Whith it, you match any \@ followed by non whitespace (\S) and insert a whitespace (what is made by replacing the whole match with '\@ ' plus the the non whitespace just found).

Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
  • This doesn't appear to work: `echo '\@.N|\@. Q' | sed -E 's/\\\@.(\S)/\\\@. \1/g'` returns `\@.N|\@. Q` (expecting `\@. N|\@. Q`; no space inserted). – simont May 09 '12 at 22:06
  • Strange, because I get the desired result. I'm running sed 4.2.1. Which version is yours? – Nicolás Ozimica May 09 '12 at 22:13
  • Good to know that. Perhaps your version of `sed` has an option to make it behave like GNU sed. But if you have `gsed` then everything is fine. – Nicolás Ozimica May 09 '12 at 22:18
1

Use this sed command:

sed -i.bak -E 's/(\\@\.)([A-Z])/\1 \2/g' *.tex

OR better:

sed -i.bak -E 's/(\\@\.)([^ \t])/\1 \2/g' *.tex

which will insert space if \@. is not followed by any white-space character (not just capital letter).

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

I think the following would be correct:

s/\\@\.[^\s]/\\@. /g

Only replace the expression if it is not followed by a space.

Flexo
  • 87,323
  • 22
  • 191
  • 272
john
  • 141
  • 4
  • This doesn't appear to work: `echo '\@.N|\@. Q' | sed -E 's/\@.[^\s]/\@. /g'` gives me `\@. |\@. Q` (chewed the N). – simont May 09 '12 at 22:04
0

This might work for you:

sed -i .bak -E 's/\\@\. \?/\\@. /g' *.tex

Explanation:

If there's a space there replace it with a space, otherwise insert a space.

potong
  • 55,640
  • 6
  • 51
  • 83