简体   繁体   English

使用批处理删除文本文件中特定单词之前的文本

[英]Delete text before specific word in text file using batch

Here's an example of what I'm looking for.这是我正在寻找的一个例子。 Let's say we have a text file that contains the following:假设我们有一个包含以下内容的文本文件:

eggs bacon
delete me here
test me 
delete this too here

I want to have a batch file that deletes everything before the word "here" but only everything on the same line.我想要一个批处理文件,删除“这里”一词之前的所有内容,但只删除同一行中的所有内容。

So the output should be:所以 output 应该是:

eggs bacon
here
test me
here

I've tried using for loops to parse the text token by token but it's getting me nowhere.我尝试使用 for 循环逐个解析文本标记,但它让我无处可去。

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q66181497.txt"
SET "outfile=%destdir%\outfile.txt"

SET "dropbefore=here"

(
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
 rem transfer line read to variable "line" for manipulation
 SET "line=%%b"
 IF "%%b" equ "!line:*%dropbefore%=!" (
  rem target text missing from line
  ECHO %%b
 ) ELSE (
  rem target found on line. replace all-before-target with target
 ECHO !line:*%dropbefore%=%dropbefore%!
 )
)
)>"%outfile%"
GOTO :EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中的文本文件中删除特定单词之前的一行 - How to delete one line before a specific word in a text file in python 批处理文件删除文本文件的前3行 - Batch file to delete first 3 lines of a text file 批处理脚本以从文本文件中读取特定单词并写入另一个文件 - Batch script to read specific word from a text file and write to another file 批处理文件替换文本文件中的文本,但保留第一个单词/列 - Batch File Replace text in text file but keep first word/column 使用erlang在文本文件中特定行的末尾写一个单词 - Write a word at the end of a specific line in a text file using erlang 从文本文件中的一行删除单词 - Delete word from a line in a text file Python如何删除文本文件中特定字符串之后或之前的特定行数 - Python how to delete a specific amount of lines after or before specific string in text file 如何使用索引删除文本文件python中的特定行? - How to delete a specific line in text file python using indices? 使用php删除文件中包含特定单词的行之前的所有行 - Delete all lines before a line that included specific word in a file by using php 如何在特定行或特定单词之后将文本插入文本文件? - How to insert text into a text file on a specific line or after a specific word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM