Reputation: 251
I want to convert
Some text $ latex code $ some more text
to
Some text $latex code$ some more text
using sed (or something else) with reasonable efficiency.
I tried to remove the first space using
sed -e 's/\$\s*/\$/g'
but that makes $ some
to $some
which is undesirable. Likewise I can do it for the second $, but that will also create similar issue at the front.
I then tried capturing group like this:
sed -e 's/\$\s*\(.*\)\s*\$/\$\1\$/g'
which is not working (maybe because (.*) group includes \s and $?). Its output is
Some text $latex code $ some more text
So how can I do this efficiently? It would be great if the answer can also accommodate $$'s spanning two lines.
Upvotes: 1
Views: 125
Reputation: 88654
With GNU sed:
sed 's/\$ \+/$/; s/ \+\$/$/2' file
or with \s
(matches space and tab):
sed 's/\$\s\+/$/; s/\s\+\$/$/2' file
Output:
Some text $latex code$ some more text
\+
: means that the preceding expression must match at least one time or multiple times
$
: dollar sign, no special meaning here
2
: only replace the 2nd match of the regex
Upvotes: 2