I need to strip a part of a text file and write it to another one. The stripped text file should be saved as well.
Before:
Original.txt = "<xml><something>values</something><script>TEXT-TO-DELETE</script></xml>"
After:
Original.txt = "<xml><something>values</something><script></script></xml>"
Original_script.txt = "TEXT-TO-DELETE"
.. this is a multi-line file, so the script section is spanning many lines.
I tried from scripts that were posted, but can never understand the syntax.
"@echo off & setlocal
set "Data=D:\Original.txt"
set "Bak=%Data%.bak"
move "%Data%" "%Bak%"
for /f "usebackq delims=" %%i in ("%Bak%") do set "LINE=%%i" & call :ProcessLine
REM del "%Bak%"
goto :eof
:ProcessLine
echo %LINE%>>"%Data%"
echo %LINE%|findstr /b "<script>">nul || goto :eof
goto :eof"
This should be for windows computers .. (newer OS support)
EDIT: since i cannot answer on my own thread, I do it here:
Thanks for the answer. Unfortunately I don't want to use shareware for this. I am parsing in another language, I just need to strip this.
This is a ridiculous 3-liner:
file={"p1.txt","p2.txt","p1.txt"}
find={"<search>","</search>,"<xml>"}
count=1
foreachline in file[count] print line to file[count]
if find[count] in line then count++
or similar. If there wouldn't be 1000 ways/styles of scripting, I could figure this myself. Just thought some crack would give it a minute.
I think an XSLT processor with a CLI is the most appropriate tool here. AltovaXML2011 is the one I use most frequently now.
The script can then be something like this, to extract the contents of the script
elements to your Original_script file, and then update your original file:
@echo off
set orig="D:\Original.txt"
set script="D:\Original_script.txt"
set backup="D:\Original_backup.txt"
set xsltprocessor="D:\AltovaXML.exe"
move %orig% %backup%
::extract scripts
%xsltprocessor% -xslt1 "D:\ExtractScripts.xslt" -in %backup% -out %script%
::update original
%xsltprocessor% -xslt1 "D:\UpdateOriginal.xslt" -in %backup% -out %orig%
::del %backup%
With ExtractScripts.xslt looking like this, it extracts the contents of all script
elements (adding newlines):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//script"/>
</xsl:template>
<xsl:template match="//script">
<xsl:apply-templates/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
With UpdateOriginal.xslt looking like this, it just copies the entire original file but leaves out the contents of all script
elements (already separately extracted):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//script">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
You can choose to keep or delete the backup file afterwards.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.