You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Batch脚本求助:提取文本中含指定标识的表格至新文件

Batch脚本提取指定表格数据问题

需求:读取input.txt中的表格数据,按原格式复制到output.txt。表格被其他文本包围,起始行包含(前方可能带空格的)"element"字符串,且延续至文件末尾,仅提取此部分。

input.txt示例内容

Recapitulation des donnees

Nom fichier H46 en entree       : cas.h46Nom fichier sigma en entree     : sigma.txtNom fichier Inp en sortie    : ficRes.inpNom fichier H46 en sortie    : ficRes.h46Nom fichier TXT en sortie    : ficRes.txtEtat limite                     : ELS


Es                              : 200000.00fck                             :     40.00Coefficient d'equivalence       :     13.07Eb                              :  15302.22fctm                            :      3.51types dIFferents de sections    :            1kt                              :      0.40

Caracteristiques de ferraillage a chaque niveau:



AX     AY   PhiX   PhiY   SpX    SpY    EnrX   EnrY     cX     cYType :           115.71  15.71  20.00  20.00 200.00 200.00  74.00  98.00  62.00  86.0015.71  15.71  20.00  20.00 200.00 200.00  74.00  98.00  62.00  86.00

element  type     cas      it      ep       sigaxi     sigaxs     sigayi     sigays     sigbpp     itred    AXI     AXS     AYI      AYS      ATR
  1   1        1        3     0.50     159.13     159.13     -12.08     -12.08      -0.92        0    15.71    15.71    15.71    15.71     0.00
   1   1        2        8     0.50     158.21     158.21     156.09     156.09      -2.00        4    15.71    15.71    16.11    16.11     0.00
   1   1        3       18     0.50     192.73     192.73      74.42      74.42      -2.15       61    21.81    21.81    15.71    15.71     0.00
   1   1        4        7     0.50     192.68     -52.76       0.00       0.00      -7.78      196    35.31    15.71    15.71    15.71     0.00
   1   1        5       21     0.50     192.99     -31.15      30.25     155.89      -9.88      266    42.31    15.71    15.71    34.41     0.00
   1   1        6        9     0.50       0.00       0.00     156.08      -9.88      -4.73      228    15.71    15.71    38.51    15.71     0.00
   1   1        7       30     0.50     193.14      28.28      22.66     155.85      -9.69      225    38.21    15.71    15.71    23.41     0.00
   1   1        8        9     0.50       0.00       0.00     -20.49     155.91      -5.75      118    15.71    15.71    15.71    27.51     0.00
   1   1        9        1     0.50     159.13     159.13       0.00       0.00       0.00        0    15.71    15.71    15.71    15.71     0.00
   1   1       10        1     0.50       0.00       0.00     156.15     156.15       0.00        3    15.71    15.71    16.01    16.01     0.00
   1   1       11        8     0.50     158.51     158.51     155.80     155.80      -1.00        4    15.71    15.71    16.11    16.11     0.00
   1   1       12        8     0.50     158.51     158.51     155.80     155.80      -1.00        4    15.71    15.71    16.11    16.11     0.00
   1   1       13       20     0.50     185.89      -5.65     155.97      -5.36     -11.36      317    34.91    15.71    47.41    15.71     0.00
   1   1       14       20     0.50     185.89      -5.65     155.97      -5.36     -11.36      317    34.91    15.71    47.41    15.71     0.00
   1   1       15        1     0.50     159.13     159.13      95.48      95.48       0.00        0    15.71    15.71    15.71    15.71     0.00

我尝试的脚本

@echo off 
set local enable delayed expansion

set "input_file=input.txt"
set "output_file=output.txt"
 
:: Are we in the table?
set "in_table="

:: Temporary file to stock the data
set "temp_file=%temp%\\temp_table.txt"
del "%temp_file%" 2\>nul

:: Go through input.txt
for /f "delims=" %%a in (%input_file%) do (
set "ligne=%%a"

:: Does the line has "element" in it ?
echo !ligne! | find /i "element" >nul
   if !errorlevel! == 0 (
     set "in_table=1"
)

:: If we are in the table, add the line to the temporary file
if defined in_table (
    echo !ligne!>> "%temp_file%"
)
 
)
 
:: Copy the temporary file to the output file
type "%temp_file%" \> "%output_file%"

:: Delete temporary file
del "%temp_file%" 2\>nul

 
endlocal

问题分析与修正脚本

原脚本存在几处语法错误,导致无法正常运行:

  • set local enable delayed expansion 语法错误,正确写法是 setlocal enabledelayedexpansion
  • 临时文件路径中的%temp%\\temp_table.txt多了一个反斜杠,应改为%temp%\temp_table.txt
  • 重定向符号前的多余反斜杠:type "%temp_file%" \> "%output_file%" 应改为 type "%temp_file%" > "%output_file%"
  • del "%temp_file%" 2\>nul 中的反斜杠多余,应改为del "%temp_file%" 2>nul

修正后的完整脚本:

@echo off 
setlocal enabledelayedexpansion

set "input_file=input.txt"
set "output_file=output.txt"
 
:: 标记是否进入表格区域
set "in_table="

:: 临时文件存储提取的表格数据
set "temp_file=%temp%\temp_table.txt"
del "%temp_file%" 2>nul

:: 逐行读取输入文件
for /f "delims=" %%a in (%input_file%) do (
    set "ligne=%%a"

    :: 检查当前行是否包含"element"(不区分大小写)
    echo !ligne! | find /i "element" >nul
    if !errorlevel! == 0 (
        set "in_table=1"
    )

    :: 如果已进入表格区域,将当前行写入临时文件
    if defined in_table (
        echo !ligne!>> "%temp_file%"
    )
)
 
:: 将临时文件内容复制到输出文件
type "%temp_file%" > "%output_file%"

:: 删除临时文件
del "%temp_file%" 2>nul

endlocal

这个脚本会从包含"element"的行开始,将后续所有行(直到文件末尾)按原格式提取到output.txt中,符合需求。

内容的提问来源于stack exchange,提问作者Killian Manach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:54:52