sd
sd copied to clipboard
Error: invalid regex regex parse error: unrecognized escape sequence when used with xargs
I have two files
- main.tex
\documentclass[12pt,a4paper]{article}
\begin{document}
Reading a Compact Disk requires a disk drive.
\end{document}
- glossary.tex
\newglossaryentry{disdr}{name={disk drive},description={A device.}}
\newglossaryentry{cd}{name={Compact Disk},description={A disk.}}
This command works:
cat ./glossary.tex | sd '.*\{(\w+)\}\{.*name=\{(.*?)\},.*|$' '"$2" "\gls{$1}" ./main.tex' | xargs -L 1 sd
resulting
\documentclass[12pt,a4paper]{article}
\begin{document}
Reading a \gls{cd} requires a \gls{disdr}.
\end{document}
But this one
cat ./glossary.tex | sd '.*\{(\w+)\}\{.*name=\{(.*?)\},.*|$' '"$2" "\gls{$1}"' | xargs -L 1 -I _ sd _ "./main.tex"
produces an error:
Error: invalid regex regex parse error:
disk drive \gls{disdr}
^^
error: unrecognized escape sequence
Error: invalid regex regex parse error:
Compact Disk \gls{cd}
^^
error: unrecognized escape sequence
First of all xargs isn’t used correctly.
-I _ sd _ "./main.tex":
sd interpretes the _ as a single parameter. Basically this happens: sd "disk drive \gls{disdr}" "./main.tex"
This would work instead:
cat ./glossary.tex | sd '.*\{(\w+)\}\{.*name=\{(.*?)\},.*|$' '"$2" "\gls{$1}"' | xargs -L 1 bash -c 'sd "$0" "$1" "./main.tex"'
But perhaps the author did that on purpose to show us the error.
Therefore, the problem reduces to:
Why does this work: sd "x" "\g" ./main.tex
But this not:
$ sd "\g" "x" ./main.tex
Error: invalid regex regex parse error:
\g
^^
error: unrecognized escape sequence
If I am not mistaken the answer is as easy as this:
- The first argument is parsed as regex pattern, and therefore it needs to be escaped.
- The second argument is not a regex pattern, therefore it is not needed there.
Thanks for the excellent explanation @Linus789