[英]Insert space after period using sed
I've got a bunch of files that have sentences ending like this: \\@.Next sentence
. 我有一堆文件的句子结尾是这样的:
\\@.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. 编辑:记录下来,我使用的是
sed
的BSD版本(我使用的是OS X)-从上一个有关sed
问题开始 ,显然BSD sed(或至少是Apple版本) 与sed
并不总是兼容GNU sed正则表达式。
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). 顺便说一句,您匹配任何
\\@
后跟非空格( \\S
)并插入一个空格(通过将整个匹配项替换为'\\@ '
加上刚刚找到的非空格来完成)。
Use this sed command: 使用以下sed命令:
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). 后面不能跟任何空格字符 (不只是大写字母)。
I think the following would be correct: 我认为以下是正确的:
s/\\@\.[^\s]/\\@. /g
Only replace the expression if it is not followed by a space. 如果表达式后面没有空格,则仅替换该表达式。
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. 如果有空格,请用空格替换,否则请插入空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.