Reputation: 135
I am doing find-replace operations using "sed" in Linux. I have a XML file in Linux named config.xml. The file contains data as followed-
<CATALOG>
<SERVER>
<URL value="http://ip-172-44-0-92.compute.internal:440/" />
</SERVER>
</CATALOG>
I want to find a line in the config.xml file that contains <URL value=
and replaces the entire line with <URL value="http://ip-181-40-10-72.compute.internal:440/" />
I tried it by executing the command-
sed -i '/<URL value=/c\<URL value=\"http://ip-181-40-10-72.compute.internal:440/\" />' config.xml
The command executes correctly and does find replace operation but, when I open the file using vi config.xml
I see ^M
character at the end of each lines that were not replaced. Why did this happened and, how to fix it?
EDIT-
By referring to @Atalajaka's answer...
My original file contains CRLF line endings at the end of each line. And, sed replaces the line with LF line ending. As a result, all other unreplaced lines will still have CRLF ending and 'vi' editor will now show ^M
at the end of these lines.
So the solution is to replace CRLF endings with LF endings by running the command-
sed -i $'s/\r$//' config.xml
Upvotes: 1
Views: 1976
Reputation: 125
The issue could rely on the original XML file, in case it has CRLF endings for each line. If that is the case, vim
will recognize and hide them, so that they remain unimportant to you. Assuming this, all new lines added with vim
will contain those same CRLF line-endings.
The sed
command uses LF line-endings when adding any new lines, so when that happens, vim
sees the two different line-endings, and will assume the LF line-endings as the regular ones. This means that all CR line-endings will be displayed as ^M
.
If you have access to the original XML file before being edited, you can open it in vim
and check if you see [dos]
at the footer, right next to the file name, something as:
$ vim original_xml.xml
...
"original_xml" [dos] ...
Upvotes: 1