I want to add whitespace to each match of a regex pattern found in a text file. Example:xadsfa 1 - 2 --- --- --- ABjklhwj 3 --- --- --- asdfhweriulceh 2 - 4 --- --- --- asdfI want to match '1 -2' or '2 - 4'. I've come up with this pattern:[0-9]\s-\s[0-9]Now I'd like to append a white space to each match, like this:xadsfa 1 - 2  --- --- --- ABjklhwj 3 --- --- --- asdfhweriulceh 2 - 4  --- --- --- asdf
Command-line command to append white space to regex match
text processing;regular expression
This is trivial with a tool like sed:$ sed 's/[0-9]\s-\s[0-9]/& /' filexadsfa 1 - 2  --- --- --- ABjklhwj 3 --- --- --- asdfhweriulceh 2 - 4  --- --- --- asdfThe s/foo/bar/ operator will replace the first occurrence of foo with bar on each line of the input file. If you want to match all cases on a given line, use g: s/foo/bar/g. The & is whatever was matched, so s/[0-9]\s-\s[0-9]/& / will replace the first match of your regex with whatever was matched plus a single space. Note that, if you're just matching a single space (not tabs or other whitespace), there's no reason to use \s. Just use spaces:sed 's/[0-9] - [0-9]/& /' fileFinally, to make the changes in the file itself, use -i (this assumes GNU sed, the syntax is slightly different on other implementations; also, this will replace your file with the result of the sed operation. Make sure it does what you need before adding -i):sed -i 's/[0-9] - [0-9]/& /' file