Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".

I want to find and replace all the strings in the file starting with Ticket_ with something called Ticket_Final.

Example, replace "Ticket_Release1" and "Ticket_V2" with "Ticket_Final" by searching for string Ticket_

Can you please help me here.

share|improve this question
3  
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc... –  jasonwryan Jul 28 at 6:47
1  
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes (") part of the string? Should we assume that a string is anything starting with Ticket_ until the next whitespace character? How about fooTicket_1? Should that be counted? Please edit your question and clarify. –  terdon Jul 28 at 7:20

1 Answer 1

You could use sed:

sed -i.BACKUP 's/\"Ticket_.*\"/\"Ticket_Final\"/g' <xml_file>

Explanation:

-i.BACKUP : It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.

s/ORIGINAL_REGEXP/SUBSTITUTION/g : Substitutes (s) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g).

\"Ticket_.*\" : Anything like Ticket_... between quotation marks.

share|improve this answer
2  
\"Ticket_.*\" will match the longest possible string between tso quotes, better to use \"Ticket_[^"]*. –  User112638726 Jul 28 at 7:17
    
You are absolutely right, it will get every string till the final " it finds. But I think it is a must to still check for the first " it encounters, like \"Ticket_[^"]*\". But again it will accept anything just like "Ticket_" which I don't know if it is a requirement. –  Isaac Jul 28 at 7:27
    
Your global flag is not useful here. –  mikeserv Jul 28 at 7:40
    
Would you mind explaining why? As far as I have used sed, which is very basic, that g is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted. –  Isaac Jul 28 at 7:43
    
well, if there are several "Ticket_.*" matches on the same line, .* eats (at least) from the first through the last, and 1 is the most you can get out of it anyway. –  mikeserv Jul 28 at 7:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.