Reputation: 31
I have a line like this
"RU_28" "CDM_279" "CDI_45"
"RU_567" "CDM_528" "CDI_10000"
I want to obtain the result below
"RU_28" "CDM_Unusued" "CDI_45"
"RU_567" "CDM_Unusued" "CDI_10000"
Do this for all the lines in the file
I'm using this commands:
sed 's/\"CDM_\w*\"/\"Unusued\"/g' File1.txt > File2.txt
It doesn't seem to works.
Thanks in advance!!!
Upvotes: 3
Views: 5133
Reputation: 14014
You're not actually leaving "CDM_" on the right side. Your substitution says "Replace CDM_ and any number of words with Unusued." The common way to do what you actually want to do is to use parentheses around the section on the left you want to keep, and then use backreferences to indicate where they go on the right. In this case, you just need a single backreference, indicated by \1
:
sed 's/"\(CDM_\)\w*"/"\1Unusued"/g' File1.txt > File2.txt
Note the backslashes before the parentheses on the left side; these can be omitted if using sed
with -r
(I think), for extended regexps, but as-is, they're necessary so sed
knows they're not literal.
Edit: I've updated the command in response to the accurate comment by Birei, noting that the extraneous escapes for the double-quotes. (Note that the ones for the parentheses are still necessary).
Upvotes: 1
Reputation: 785541
You can use:
sed -i.bak 's/"\(CDM_\)[^"]*"/"\1Unused"/' file1.txt
Upvotes: 2