简体   繁体   中英

Windows batch script to move lines from txt file to new file

I have a load of text files with some html code in them. EG:

Some random text....
..
...
....
<tag1>some more random text</tag1>
....
...
..

I need to run a script to go through each text file and move each line between the tags to a new text file in the same folder, and remove them from the original.

So the end result would be one file with no <tag1> and another file with only <tag1> .

I hope I made myself clear enough. Is this at all possible?

IF (and that is a big, bolded, italicized, capitalized IF ) you can guarantee that each <tag1>...</tag> tag appears on a single line with no other content on that same line, and there are no tag attributes to complicate things, then the answer is easy.

Edit - fixed a number of bugs, it actually works now :-)

@echo off
for %%F in (*.txt) do (
  echo processing %%F
  findstr /rc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.tag1%%~xF"
  findstr /rvc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.new%%~xF"
  >nul move /y "%%~nF.new%%~xF" "%%F"
)

The solution could be extended to handle tag attributes fairly easily.

But I seriously doubt your problem is really that simple. Valid HTML content can have a tag spread accross many lines, and there can be many tags on one line.

Windows native batch is pretty lousy at text processing in general, and even worse for HTML or XML. I strongly recommend getting a third party tool like gnu sed for Windows that has robust text processing. Or better yet, get a tool that is specifically designed to process HTML.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM