I got a huge LaTeX file with bad references to figures, for example it says
我得到了一个巨大的乳胶文件,其中有对数字的错误引用,例如它说
"... So in the figure 3-12 we see ... similar to figure 3-1..."
where it should be
在它应该在的地方
"... So in the figure \ref{fig:3-12} we see ... similar to figure \ref{fig:3-1}..."
For save time, silly me, I tought a find/replace like:
为了节省时间,我真傻,我应该找个像这样的人来代替。
find: figure 3-
replace: figure \ref{fig:3-
查找:图3-替换:图\ref{图3-
Which returned:
返回:
"... So in the figure \ref{fig:3-12 we see ... similar to figure \ref{fig:3-1..."
now the closing brackets
现在关闭括号
sed 's#/ref{fig:3-\d+#\ref{fig:3-\d+}#g' main.tex
which yields
的收益率
"... So in the figure \ref{fig:3-\d+} we see ... similar to figure \ref{fig:3-\d+}..."
What is wrong friends?. Thank you.
怎么了朋友?。谢谢你!
2 个解决方案
#1
1
You may capture any streaks of digits and hyphens after the word figure
:
在“图”一词后,你可以捕捉到任何数字和连字符的条纹:
s="... So in the figure 3-12 we see ... similar to figure 3-1..."
echo $s | sed -E 's#(figure +)([0-9-]+)#\1\\ref{fig:\2}#g'
See the online demo.
看到在线演示。
Example on how to do the inplace replacement with a *.bak copy (tested in Ubuntu):
如何用*替换inplace。bak副本(在Ubuntu中测试):
sed -i.bak -E 's#(figure +)([0-9-]+)#\1\\ref{fig:\2}#g' main.tex
Details
细节
-
(figure +)
- Group 1: captures thefigure
substring and 1 or more spaces (replace with[[:blank:]]+
to match any spaces or tabs) - (图+)-组1:捕获图子字符串和1或更多的空格(用[[[:blank:]]]]+替换以匹配任何空格或制表符)
-
([0-9-]+)
- Group 2: one or more digits or-
. - ([0-9-]+)-第2组:一个或多个数字或-。
The replacement is:
更换:
-
\1
- a replacement backreference to Group 1 value - \1 -替换对组1值的回引用
-
\\ref{fig:
- a literal\ref{fig:
substring (the backslash must be escaped as it is a "special" char) - \\ref{fig: -一个文字\ref{fig: substring(反斜杠必须转义,因为它是一个“特殊”字符)
-
\2
- a replacement backreference to Group 2 value - \2 -对组2值的替换回引用
-
}
- a}
char. - } -字符。
#2
1
You can use this sed
:
您可以使用这个sed:
s="... So in the figure 3-12 we see ... similar to figure 3-1..."
sed -E 's/[0-9]+-[0-9]+/\\ref{fig:&}/g' <<< "$s"
... So in the figure \ref{fig:3-12} we see ... similar to figure \ref{fig:3-1}...
So we are matching a pattern [0-9]+-[0-9]+
to match number-number
pattern. In the replacement &
is back-reference of fully matched string by regex pattern.
所以我们正在匹配一个模式[0-9]+-[0-9]+来匹配数字模式。在替换中&是正则表达式模式对完全匹配字符串的回引用。
#1
1
You may capture any streaks of digits and hyphens after the word figure
:
在“图”一词后,你可以捕捉到任何数字和连字符的条纹:
s="... So in the figure 3-12 we see ... similar to figure 3-1..."
echo $s | sed -E 's#(figure +)([0-9-]+)#\1\\ref{fig:\2}#g'
See the online demo.
看到在线演示。
Example on how to do the inplace replacement with a *.bak copy (tested in Ubuntu):
如何用*替换inplace。bak副本(在Ubuntu中测试):
sed -i.bak -E 's#(figure +)([0-9-]+)#\1\\ref{fig:\2}#g' main.tex
Details
细节
-
(figure +)
- Group 1: captures thefigure
substring and 1 or more spaces (replace with[[:blank:]]+
to match any spaces or tabs) - (图+)-组1:捕获图子字符串和1或更多的空格(用[[[:blank:]]]]+替换以匹配任何空格或制表符)
-
([0-9-]+)
- Group 2: one or more digits or-
. - ([0-9-]+)-第2组:一个或多个数字或-。
The replacement is:
更换:
-
\1
- a replacement backreference to Group 1 value - \1 -替换对组1值的回引用
-
\\ref{fig:
- a literal\ref{fig:
substring (the backslash must be escaped as it is a "special" char) - \\ref{fig: -一个文字\ref{fig: substring(反斜杠必须转义,因为它是一个“特殊”字符)
-
\2
- a replacement backreference to Group 2 value - \2 -对组2值的替换回引用
-
}
- a}
char. - } -字符。
#2
1
You can use this sed
:
您可以使用这个sed:
s="... So in the figure 3-12 we see ... similar to figure 3-1..."
sed -E 's/[0-9]+-[0-9]+/\\ref{fig:&}/g' <<< "$s"
... So in the figure \ref{fig:3-12} we see ... similar to figure \ref{fig:3-1}...
So we are matching a pattern [0-9]+-[0-9]+
to match number-number
pattern. In the replacement &
is back-reference of fully matched string by regex pattern.
所以我们正在匹配一个模式[0-9]+-[0-9]+来匹配数字模式。在替换中&是正则表达式模式对完全匹配字符串的回引用。