Input file:
__<name>AWEETET</name>
____<name_evidence="3"_type="2@#">QEWQE</name>
__<name>QWE048</name>
____<name_evidence="3"_type="570">@#@$#545</name>
____<name_evidence="2"_type="351">QWE4</name>
Desired output:
__<tmp>AWEETET</tmp>
____<name_evidence="3"_type="2@#">QEWQE</name>
__<tmp>QWE048</tmp>
____<name_evidence="3"_type="570">@#@$#545</name>
____<name_evidence="2"_type="351">QWE4</name>
Command that I tried:
[home@perl_beginner]sed 's/^__<name>[a-z]*[0-9]*<\/name>/^__<tmp>[a-z]*[0-9]*<\/tmp>/g' input_file.txt
Thanks for any advice.
agama
2
In your replacement you cannot use expressions such as .* or [0-9]. You need to put parentheses round the portion(s) of the pattern that you wish to "copy into the replacement string when the pattern is matched. Back references (\1 in this case) are used in the replacement to show where the contents of the portion of the string that matched inside of the parentheses are to be copied.
Given that, this might work:
sed 's/^__<name>\(.*\)<\/name>/__<tmp>\1<\/tmp>/' data-file
Additionally, words following <name> are in UPPER case, the above highlighted would match the lower cases only and [a-z]*[0-9]* sequence tells that zero or more occurrences of alphabets followed by zero or more occurrences of numeric, which may sometimes fail if the word is an alpha-numeric as in QWE048TRR. This could be rectified by giving the regex as [A-Z0-9]*
sed 's/__<name>\([A-Z0-9]*\)<\/name>/__<tmp>\1<\/tmp>/' inputfile