3

I have strings in a text file with certain formats. I want to replace the word part of the string but keep the numeric identifier. For example, I have strings like L2_name1, L12_name6 but many of the names have similar identifiers: name1 = JohnMSmith, name6 = JohnMSmithinson.

I would like to use something like sed 's/L2_name1/L2_new_name/g' file.csv but using regex so something like

sed -E 's/L[0-9]{1,2}_name1/{same leading monicker found in the matching part}_new_name/g' file.csv

  • You should read about capture groups, this answer already explains how to use them. https://unix.stackexchange.com/a/263675/31760 – X Tian Sep 11 '19 at 17:09

2 Answers2

3

You remember things by parenthesizing what you want to remember in the match part of the command and you recall the remembered things using \N with some number N to replace it with what the Nth set of parens in the matching part remembered:

sed -E 's/(L[0-9]{1,2})_name([0-9]+)/\1_new_name\2/g' file.csv

See the sed manual for the full details.

EDIT: I should point out that you are allowed to use various special chars like ()[]{}+ without escaping them because you have specified -E i.e. extended regular expressions. Again the manual provides full details.

NickD
  • 2,926
3

You can use \0 for referring to the matched string.

echo "test string this is a test string" | sed -E 's/test string(.+?)test string/\0 success/'

Look up the topic capture-groups in regex for more info.

  • How does this answer the question? … … … … … … … … … … … … … … … Please do not respond in comments; [edit] your answer to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Jul 05 '22 at 03:08