问题
I have an xml format file for bulk loading in SQL Server 2008. I need to replace the instance of TERMINATOR="\r\n"
with TERMINATOR="\n"
. I'm trying to use the command line utility FART.exe
. No matter how I try, I cannot seem to get the utility to recognize the characters.
I have tried passing as combos of the find string and replace string:
\r\n \n
"\r\n" "\n"
""\r\n"" ""\n""
^\r^\n ^\n
TERMINATOR=""\r\n"" TERMINATOR=""\n""
TERMINATOR=""^\r^\n"" TERMINATOR=""^\n""
and many, many more combinations. Every article I look up about escaping DOS
commands tells me something different and I've tried them all with no luck. The truly strange thing is that not only does using \r\n \n
not do what I want it to do (replace the literals) but it does not replace the carriage return + line feed characters at the end of each line in the file.
The format file I need to edit. I believe the line I've underlined in red is causing a problem:
The command window output from calling Jrepl. It's obvious that the utility has a problem with the red-underlined line from the other screenshot:
回答1:
To replace all \r\n
literals with \n
, simply use:
fart yourFile.xml \r\n \n
To only replace within larger string TERMINATOR="\r\n"
, then use:
fart -C yourfile.xml TERMINATOR=\x22\\r\\n\x22 TERMINATOR=\x22\\n\x22
I prefer to use JREPL.BAT - It is much more powerful than FART in that it can use regular expressions, plus it has a number of additional advanced features. The only limitation relative to FART is it can only process one file at a time.
To replace all \r\n
literals with \n
:
jrepl \r\n \n /l /f yourFile.xml /o -
To be more specific using a literal search string with quote escaped as \q
and backslash as \\
:
jrepl "TERMINATOR=\q\\r\\n\q" "TERMINATOR=\q\\n\q" /x /l /f yourFile.xml /o -
or using a regular expression:
jrepl "(TERMINATOR=\q)\\r(?=\\n\q)" "$1" /x /f yourFile.xml /o -
回答2:
Simplest and quickest is fart binary method, for example:
fart.exe -C D:\sample.txt \x0d\x0a \x0a
来源:https://stackoverflow.com/questions/36267389/how-to-replace-r-n-literal-with-n-using-fart-exe