批处理文件,用于删除txt文件中每行的前缀和后缀

时间:2023-01-14 19:27:01



I have a txt file contains certain lines with constant structure, for example:

我有一个txt文件包含具有常量结构的某些行,例如:

My Line is : Hi. This is row no.1.
My Line is : Hi. This is row no.2
My Line is : Hi. This is row no.3.

I would like to create a new file contains same rows, but to delete all characters before specific substring (for example: "Hi"),
as well as the last specific character of this row (for example: "."), only if exists.

The final txt file I expect for is:

我想创建一个包含相同行的新文件,但要删除特定子字符串之前的所有字符(例如:“Hi”),以及此行的最后一个特定字符(例如:“。”),仅限于存在。我期望的最终txt文件是:

Hi. This is row no.1
Hi. This is row no.2
Hi. This is row no.3

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


0  

In batch, string replacement and substring extraction are done using the SET command. Look it up using set /? or help set. However, when using SET, there are some characters in the string that could be problematic, including !, since this solution uses delayed expansion. There are methods that don't use delayed expansion, but without a real example of the strings you're working with, I'm not sure whether you need them or not.

在批处理中,使用SET命令完成字符串替换和子字符串提取。使用set /?查找或帮助设置。但是,使用SET时,字符串中可能存在一些问题,包括!,因为此解决方案使用延迟扩展。有些方法不使用延迟扩展,但如果没有您正在使用的字符串的真实示例,我不确定您是否需要它们。

String replacement looks like this.

字符串替换看起来像这样。

%VARIABLE:original=replacement%

With delayed expansion, which allows the variable values to be determined for each iteration in the for, instead of once at the beginning for the entire for block, replace the % with !.

使用延迟扩展,允许为for中的每次迭代确定变量值,而不是在整个for块的开头一次,用%替换%。

Look at HELP SET for examples of substring expansion; there are a number of them depending on whether you index from the start or end of the string.

查看HELP SET以获取子串扩展的示例;它们中有许多取决于您是从字符串的开头还是结尾编制索引。

Here is one solution according to the sample data you provided and my comments above. For each line in test1.txt, it processes it and outputs it to output.txt.

根据您提供的示例数据和我上面的评论,这是一个解决方案。对于test1.txt中的每一行,它处理它并将其输出到output.txt。

@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in (test1.txt) do (
    set line=%%a
    set line=!line:My Line is : =!
    if "!line:~-1!"=="." set line=!line:~0,-1!
    >> output.txt echo !line!
)

#2


-1  

with cut
cut -f2 -d: infile > outfile

with cut cut -f2 -d:infile> outfile

but that leaves the trailing dot,
could pipe that to something to trim it

但是留下了尾随点,可能会将其管道以修剪它

or try sed

或尝试sed

sed 's/.*:\(.*\)\.$/\1/g' infile > outfile

but that really expects the final dot to be there.

但是真的希望最终的点在那里。

sed 's/.*: \(.*\)/\1/g;s/\.$//g' infile > outfile

seems to do it

似乎这样做

#1


0  

In batch, string replacement and substring extraction are done using the SET command. Look it up using set /? or help set. However, when using SET, there are some characters in the string that could be problematic, including !, since this solution uses delayed expansion. There are methods that don't use delayed expansion, but without a real example of the strings you're working with, I'm not sure whether you need them or not.

在批处理中,使用SET命令完成字符串替换和子字符串提取。使用set /?查找或帮助设置。但是,使用SET时,字符串中可能存在一些问题,包括!,因为此解决方案使用延迟扩展。有些方法不使用延迟扩展,但如果没有您正在使用的字符串的真实示例,我不确定您是否需要它们。

String replacement looks like this.

字符串替换看起来像这样。

%VARIABLE:original=replacement%

With delayed expansion, which allows the variable values to be determined for each iteration in the for, instead of once at the beginning for the entire for block, replace the % with !.

使用延迟扩展,允许为for中的每次迭代确定变量值,而不是在整个for块的开头一次,用%替换%。

Look at HELP SET for examples of substring expansion; there are a number of them depending on whether you index from the start or end of the string.

查看HELP SET以获取子串扩展的示例;它们中有许多取决于您是从字符串的开头还是结尾编制索引。

Here is one solution according to the sample data you provided and my comments above. For each line in test1.txt, it processes it and outputs it to output.txt.

根据您提供的示例数据和我上面的评论,这是一个解决方案。对于test1.txt中的每一行,它处理它并将其输出到output.txt。

@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in (test1.txt) do (
    set line=%%a
    set line=!line:My Line is : =!
    if "!line:~-1!"=="." set line=!line:~0,-1!
    >> output.txt echo !line!
)

#2


-1  

with cut
cut -f2 -d: infile > outfile

with cut cut -f2 -d:infile> outfile

but that leaves the trailing dot,
could pipe that to something to trim it

但是留下了尾随点,可能会将其管道以修剪它

or try sed

或尝试sed

sed 's/.*:\(.*\)\.$/\1/g' infile > outfile

but that really expects the final dot to be there.

但是真的希望最终的点在那里。

sed 's/.*: \(.*\)/\1/g;s/\.$//g' infile > outfile

seems to do it

似乎这样做