0

I am trying to replace a 10-digit number I find with regex, with the same word pre-pended with "abc". For example, if the matched number is 0123456789, I want to replace that word with abc0123456789 instead.

Googling around, I found that sed is useful for replacing words in a file -

sed -i '' -e 's/[0-9]\{10\}/new-word/g' *.csv

This command successfully replaced my 10-digit number instances with "new_word". How do I instead replace it with "abc + [matched word]" ?

1 Answer 1

-1

Use parentheses to create a "matching capture group". To refer to the first group, use \1, and so on:

s/\([0-9]\{10\}\)/abc\1/g

Or use & which stands for "the whole matching part":

s/[0-9]\{10\}/abc&/g
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.