Home > Back-end >  Windows batch script to compare two csv files
Windows batch script to compare two csv files

Time:11-15

The first csv file contains a list of hostnames. The second csv file contains hostnames and logs. I would like to compare if the list of hostname exist in the second csv file. If they do, I would like to write a log to say this hostname exist. If the hostname doesn't exist, I would like to write a log to say they hostname doesn't exist in the second csv file.

I am working on a batch file using fc command but no luck yet to get my desired results.

CodePudding user response:

for /f %e in (file1) do findstr "%e" file2 >nul&if errorlevel 1 (echo %e>>missing) else (echo %e>>found)

As a command from the prompt. If running as a batch line, change each %e to %%e.

[untested]

CodePudding user response:

I don't know the structure of your files, but I hope this PowerShell script can help:

foreach($line in Get-Content .\Hosts.csv) {
    if(Get-Content .\Logs.csv | Select-String $line){
        echo "The host $line exists!"
    }
    else{
        echo "The host $line doesn't exist."
    }
}

Where the "Hosts.csv" is the list of your hosts, and the "Logs.csv" is a file contains the hosts and their logs in each line.

  • Related