Issue
the string is ">ABCD EFG;" . i want to change >, ,;,
to _,;,!
by shell command "sed", how to do ?
i try,
echo ">ABCD EFG;" |sed 's#>(.*) (.*);$#_$1;$2!#g'
and the result is >ABCD EFG;
$1 and $2 are errors?
unuseful!
Solution
Sed use basic regular expression (BRE) by default. That mean you need to escape some character.
This >(.*) (.*);$
regular expression would work using extended regular expression (ERE, -E
), but would look like >\(.*\) \(.*\);$
using BRE.
Secondly the replacement syntax uses \
and not $
(from sed's man page) :
The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
So your sed command using BRE should look like (note that the command g
isn't mandatory, as in your example you need to do this modification once per line) :
sed 's/>\(.*\) \(.*\);$/_\1;\2!/'
Answered By - Marius_Couet Answer Checked By - Marie Seifert (WPSolving Admin)