如何获取每个第3行文本文件并使用批处理脚本将其放在单独的文本文件中

时间:2022-04-18 23:59:25

I need to get only the every 3rd line of a text file(Movie.txt) and put it on a separate text file(Title.txt) using batch script(for /f)

我需要使用批处理脚本(/ F)只得到了一个文本文件(Movie.txt)的每个第3行,把它放在一个单独的文本文件(Title.txt)

Movie.txt

 Movie
  Title
  Anime1
Movie
  Title
  Anime2
Movie
  Title
  Anime3
Movie
  Title
  Anime4

Title.txt

Anime1
Anime2
Anime3
Anime4

currently I have no idea how to get the required data... I would appreciate any help you can provide. thank you.

目前我不知道如何获得所需的数据...我希望你能提供任何帮助。谢谢。

2 个解决方案

#1


1  

@echo off
setlocal enabledelayedexpansion
set input=yourinput.txt
set output=youroutput.txt
set i=0
for /f "tokens=*" %%l in (%input%) do (
    set /a i=!i!+1
    set /a r=!i!%%3
    if !r!==0 echo %%l>>%output%
)

We can use the modulo operator and a line counter. !i! contains the line number and !r! is the line number modulo 3. So if !r!==0 we write the line (%%l) into the output file.

我们可以使用模运算符和行计数器。 !一世!包含行号和!r!是模数为3的行号。因此,如果!r!== 0,我们将行(%% l)写入输出文件。

#2


0  

@ECHO OFF
SETLOCAL
SET "line1="
SET "line2="
FOR /f "tokens=1*delims=" %%a IN (
 'findstr /r "." "100lines.txt"'
 ) DO (
 IF DEFINED line1 (
  IF DEFINED line2 (
   ECHO %%a
   SET "line1="
   SET "line2="
  ) ELSE SET line2=y
 ) ELSE SET line1=y
)
GOTO :EOF

All you need is to replace the input filename - I have a file named 100lines.txt that contains 100 different lines of text.

您只需要替换输入文件名 - 我有一个名为100lines.txt的文件,其中包含100行不同的文本。

Since a variable's current defined status acts on the run-time value of the variable, ensure that the two flags are clear to start, then for each line of the file,

由于变量的当前定义状态作用于变量的运行时值,因此请确保两个标志清除以启动,然后对于文件的每一行,

if line1 is not defined, set it
otherwise, if line2 is not defined, set it
otherwise we are on a *3 line, so regurgitate it and clear the two flags.

如果没有定义line1,则设置为否则,如果未定义line2,则设置它,否则我们在* 3线上,因此重新排它并清除两个标志。

#1


1  

@echo off
setlocal enabledelayedexpansion
set input=yourinput.txt
set output=youroutput.txt
set i=0
for /f "tokens=*" %%l in (%input%) do (
    set /a i=!i!+1
    set /a r=!i!%%3
    if !r!==0 echo %%l>>%output%
)

We can use the modulo operator and a line counter. !i! contains the line number and !r! is the line number modulo 3. So if !r!==0 we write the line (%%l) into the output file.

我们可以使用模运算符和行计数器。 !一世!包含行号和!r!是模数为3的行号。因此,如果!r!== 0,我们将行(%% l)写入输出文件。

#2


0  

@ECHO OFF
SETLOCAL
SET "line1="
SET "line2="
FOR /f "tokens=1*delims=" %%a IN (
 'findstr /r "." "100lines.txt"'
 ) DO (
 IF DEFINED line1 (
  IF DEFINED line2 (
   ECHO %%a
   SET "line1="
   SET "line2="
  ) ELSE SET line2=y
 ) ELSE SET line1=y
)
GOTO :EOF

All you need is to replace the input filename - I have a file named 100lines.txt that contains 100 different lines of text.

您只需要替换输入文件名 - 我有一个名为100lines.txt的文件,其中包含100行不同的文本。

Since a variable's current defined status acts on the run-time value of the variable, ensure that the two flags are clear to start, then for each line of the file,

由于变量的当前定义状态作用于变量的运行时值,因此请确保两个标志清除以启动,然后对于文件的每一行,

if line1 is not defined, set it
otherwise, if line2 is not defined, set it
otherwise we are on a *3 line, so regurgitate it and clear the two flags.

如果没有定义line1,则设置为否则,如果未定义line2,则设置它,否则我们在* 3线上,因此重新排它并清除两个标志。