2016-03-27 17 views
1

ÖrnekBir metin dosyasının belirli bir satırını nasıl karşılaştırabilirim?

(
Line1: echo echo example 
Line2: echo echo example 
Line3: echo echo example 
Line4: echo echo File Example 
) > File1.txt 

(
Line1: echo echo example 
Line2: echo echo example 
Line3: echo echo File 
Line4: echo echo File 
) > File2.txt 

diğer satırları atmadan onun eşit ya da değil ama eğer sadece Line4 karşılaştırmak istiyorsunuz. Böyle dosyaların bu tür karşılaştırmak için bir dizi kullanabilirsiniz bu durumda

+0

Hangi tür toplu iş dosyası? Bash senaryosu mu? Powershell komut dosyası? Python senaryosu mu? PHP betiği? –

+1

@JuanLago: Peki, fare imleci 'batch-file' etiketinin üzerine yerleştirildiğinde görüntülenen açıklamayı _read_: _" MS-DOS komut yorumlayıcısı tarafından çalıştırılan bir dizi komut içeren bir metin dosyası, IBM OS/2 veya Microsoft Windows "_... – Aacini

cevap

1
@ECHO OFF 
SETLOCAL 
SET "sourcedir=U:\sourcedir" 
SET "filename1=%sourcedir%\q36252686.txt" 
SET "filename2=%sourcedir%\q36252686_2.txt" 

:: This part simply establishes the data IN the files 

FOR %%a IN ("%filename1%" "%filename2%") DO DEL %%a 2>nul 
FOR %%a IN (
"Line1: echo echo example" 
"Line2: echo echo example" 
"Line3: echo echo example" 
"Line4: echo echo File Example" 
) DO >> "%filename1%" ECHO(%%~a 

FOR %%a IN (
"Line1: echo echo example" 
"Line2: echo echo example" 
"Line3: echo echo File" 
"Line4: echo echo File" 
) DO >> "%filename2%" ECHO(%%~a 

:: line to match 

SET /a matchonline=4 
:: lines to skip 
SET /a skiplines=matchonline-1 
SET "skiplines=skip=%skiplines%" 

IF %matchonline%==1 SET "skiplines=" 

FOR /f "usebackq%skiplines%delims=" %%a IN ("%filename1%") DO (
FOR /f "usebackq%skiplines%delims=" %%b IN ("%filename2%") DO (
    IF "%%a"=="%%b" (SET "result=true") ELSE (SET "result=false") 
    GOTO done 
) 
) 
:done 
ECHO match ON line %matchonline%=%result% 

GOTO :EOF 

Sen koşullara en uygun filename1 ve filename2 ayarlarını değiştirmesi gerekiyordu. Verilerinizi test için sistemimde uygun dosya isimlerine dönüştürdüm.

İlk dosyayı okuyun, bir önceki ile eşleşen satırları atlayın ve içeriğini %%a'a atayın. Bu olduğunda, ikinci dosya için tekrarla %%b

Sonucu ayarlayın ve döngüden çıkın.

2

sahte Dönüş:

@echo off 
(
    echo Line1:example 
    echo Line2:example 
    echo Line3:example 
    echo Line4:File 
)> File1.txt 

(
    echo Line1:example 
    echo Line2:example 
    echo Line3:File 
    echo Line4:example 
)> File2.txt 

setlocal enabledelayedexpansion 
set i=0 
for /f "tokens=* delims=" %%a in ('Type "File1.txt"') do (
    set /a i+=1 
    set File1_Line[!i!]=%%a 
) 
set File1_Line[4] 
pause 
::******************************************************** 
set i=0 
for /f "tokens=* delims=" %%a in ('Type "File2.txt"') do (
    set /a i+=1 
    set File2_Line[!i!]=%%a 
) 
set File2_Line[4] 
pause 
::******************************************************** 
echo File1_Line[4]" = !File1_Line[4]!" 
pause 
echo File2_Line[4] = "!File2_Line[4]!" 
pause 
::******************************************************** 
IF /I "!File1_Line[4]!" equ "!File2_Line[4]!" (echo TRUE 
) else (
echo False 
) 
pause 
İlgili konular