Sometimes we want to search for a pattern and use that pattern in replace as well. Like the substitution is an extension of what we already have in the file.
E.g. append all employee ages with 'years'
While I was typing this up, I thought let me try using a second pattern as well.
E.g. append all employee ages with 'years'
Commandemp_name:Jane emp_age:25
emp_name:John emp_age:22
emp_name:Mary emp_age:30
emp_name:Natalie emp_age:29
other data here
Breaking it up -:%s/\(emp_age.*\)/\1 years
/g
emp_name:Jane
%s - apply thru out the file
\( and \) - escape the parentheses
(emp_age.*) - consider it a group
\1 - escape 1; 1 means the first pattern, in our case it's the only one
\1 years - use the first pattern and append years to it
g - replace multiple occurrences on the same line
Outputemp_name:Jane emp_age:25 years emp_name:John emp_age:22 years emp_name:Mary emp_age:30 years emp_name:Natalie emp_age:29 years other data here
While I was typing this up, I thought let me try using a second pattern as well.
:%s/\(emp_age.*\)\(years\)/\1- \2/g
If we run this in the above output we getHope this helps.emp_name:Jane emp_age:25 - years emp_name:John emp_age:22 - years emp_name:Mary emp_age:30 - years emp_name:Natalie emp_age:29 - years other data here