我有一个包含磁带*的数千个文本文件的目录(它们是旧Fortran程序的输出文件),我需要从每个文件中提取4行以及从中提取的文件名。 4行从可预测的字符串开始向下4行,但我们可以在此示例中使用“Header”:
TAPE1:
First line
...
Header
Trash1
Trash2
Trash3
Data1
Data2
Data3
Data4
...
我不关心这4条数据行之前或之后的任何事情,但我也希望在每条数据行之后输出文件名(即“tape1”),如下所示:
Data1 tape1
Data2 tape1
Data3 tape1
Data4 tape1
Data5 tape2
Data6 tape2
...
对于一个简单的Windows批处理文件,如何在目录中的所有磁带*文件上执行此操作?
答案 0 :(得分:3)
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=:" %%a in ('findstr /N "Header" tape*') do (
(for /L %%i in (-2,1,%%b) do set /P "="
for /L %%i in (1,1,4) do set /P "line=" & echo !line! %%a
) < "%%a"
)
答案 1 :(得分:0)
您可以尝试以下方法:
@echo off
for %%F in (".\tape*.txt") do (
for /F "delims=:" %%N in ('findstr /N /X /C:"Header" "%%~F"') do (
call :SUB "%%~F" %%N
)
)
exit /B
:SUB
set /A "SKIP=%~2+3"
for /F "skip=%SKIP% usebackq delims=" %%L in ("%~1") do (
echo(%%L %~n1
)
exit /B
这是另一种方法:
@echo off
for %%F in (".\tape*.txt") do (
for /F %%C in ('^< "%%~F" find /C /V ""') do (
set /A "POS=0" & set "NAME=%%~nF"
< "%%~F" (
setlocal EnableDelayedExpansion
for /L %%I in (1,1,%%C) do (
set "LINE=" & set /P LINE=""
if "!LINE!"=="Header" (
set /A "POS=%%I+3"
)
if !POS! GTR 0 if %%I GTR !POS! (
echo(!LINE! !NAME!
)
)
endlocal
)
)
)
答案 2 :(得分:0)
name = input("What is your name? ")
while True:
try:
income = int(input("What is your income? ($) "))
break
except ValueError:
print ("Invalid input\nPlease enter a figure")
continue
else:
break
M1 = "This program cannot determine your tax code. Please use the program for secondary income "
print("Please answer the questions with 'yes' and 'no'")
Q1=input("Do you receive an income tested benefit? ")
while Q1 != "yes" and Q1 != "no":
print("\nPlease enter either 'yes' or 'no'")
Q1=input("Do you recieve an income tested benefit? ")
if Q1=="yes":
Q2=input("Is this tax code for the income tested benefit? ")
while Q2 != "yes" and Q2 != "no":
print("\nPlease enter either 'yes' or 'no'")
Q2=input("Is this tax code for the income tested benefit? ")
if Q2=="yes":
TaxCode = "M"
elif Q2=="no":
print (M1)
print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)