The solution to the new problem isn't really about the tool used, but the regex applied. Since the ";
" is a literal character that matches only once, you need to run the thing over and over. What you need is to match a string of identical characters, and for that you use the "+
" operator (match the previous character one or more times).
sed -r -i -e '/thisis/ s|;+" />$|" />|' file.txt
ed -s file.txt <<HEREDOC
g/thisis/ s/;\+" \/>$/" \/>/
Here you can see an example of a situation in which ed
can be trickier to use than sed
. We have to backslash several characters to do it, due to the lack of a configurable delimiter and default extended regex ability.
, I used the -r
option to avoid having to backslash the "+
" sign, but this option isn't available in ed
. See the regex sections of both programs for more.
On the other hand, both ed
can use multiple expressions at once. sed
with the -e
option, and ed
just by putting it into the script. So you can combine this with your other edits if you want and avoid some more extra processes.
In both cases I used "thisis" as an address string to target the line. You'll probably want to change it to something more exacting. I recommend always using an address field like this if you can, BTW. It helps to avoid errors and makes it more efficient by skipping the processing of unnecessary lines. And I made the substitution expression a bit longer, again to avoid any possible false positives. Remember too that sed
works globally by default, but ed
Also notice how single quotes escape double quotes, and vice-versa. This helps make it easier when you need to include one kind inside the text itself. Just remember that double quotes are needed in order to expand parameters and command substitutions (anything that begins with "$
Here's some vital info on arguments and quoting:
Finally, it definitely pays off in the long run to really familiarize yourself with regular expressions. It's about the most useful single tool you can study. Get yourself a good tutorial and work through it.
Here are a few regular expressions resources: