Batch file to compare the differences in two csv files - windows

I use windows 7.
I have two csv files file1.csv and file2.csv
file1.csv
emp_id;salary
1;1000
2;2000
3;3000
file.csv
emp_id;salary
1;1000
2;2000
3;3000
4;4000
5;5000
I'm confused how to write a batch file.
The batch file should output the should be a csv file showing the changes.
Sample output:
emp_id;salary
4;4000
5;5000

You can use findstr to look for differences, and the /v parameter to display differences. Like so:
findstr /v /g:"file1.csv" "file2.csv"
Also:
for /f "delims=" %%a in (file1.csv) do (
findstr "^%%a$" "file2.csv" >nul ||echo %%a
)
And using the fc command:
fc "file1.csv" "file2.csv"
For fc im sure you can use an if not errorlevel 1 echo No difference

Related

[Pentaho][Using Shell script][CMD] insert count line and table name into txt file

I am trying to writing script by using shell scripting in pentaho.
First,file name "test_viewpayment4.txt".
I have to count the line, the correct output is 100(which not include the header), and I have to get the file name also. After that,creates the txt file which include count line and file name as picture below
https://i.stack.imgur.com/afrGg.png
Here is my code in Pentaho
https://i.stack.imgur.com/mYuWw.png
CMD.EXE /C
call cd /d d:\test
call set file=test_viewpayment4.txt
call Type d:\test\test_viewpayment4.txt | more +2 | find /V /C "~~~" > d:\test\Result5.txt
call echo %file% >> d:\test\Result5.txt
Here is the result in Result5.txt
100
test_viewpayment4.txt
However,the data is not in the same line.
Any help or pointing the right direction will be really appreciate it.
use a for loop to get a command's output to a variable:
for /f %%a in ('more +2 "d:\test\test_viewpayment4.txt" ^|find /v /c ""') do set lines=%%a
then just echo all in one command:
>> d:\test\Result5.txt echo %file% %lines%
(Note: none of the call commands is necessary in batch; also cmd /c is useless. Dismiss them, except Pentaho (which I don't know) needs them)

Split text file into multiple files using windows batch scripting

I need to split one text file into multiple files using windows batch script, could anybody light me up?
sample text file:
abc1-10
abc1-11
abc1-12
xyz2-01
xyz2-02
xyz3-01
xyz3-02
in this case, it has to split into 3 files, first one consists the lines abc1-xx, second one consists xyz2-xx and xyz3-xx go to the last one
You could use a batch file, but why not just use FINDSTR command?
findstr /R "^abc1-" sample.txt > file1.txt
findstr /R "^xyz2-" sample.txt > file2.txt
findstr /R "^xyz3-" sample.txt > file3.txt
Use the cgwin command SPLIT.
Samples:
-split a file every 500 lines counts:
split -l 500 [filename.ext]
For more: split --help
This may help - it will split the text into separate files of
abc1.txt
xyz2.txt
xyz3.txt
#echo off
for /f "tokens=1,* delims=-" %%a in ('type "file.txt"') do (
>>"%%a.txt" echo(%%a-%%b
)
pause

Output difference of 2 txt files to a 3rd txt file

I'm trying to run a bat file that will compare 1 file to another and output the differences
I've tried using gnu diff utilites, fc, and endless googleing to find a solution but I cant seem to figure it out
File 1
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
File 2
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
I want file 3 to be
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
Any one have any ideas?
You could use diff from the DiffUtils and something like this:
diff file1.txt file2.txt | findstr /r /c:"^<" /c:"^>" >file3.txt
The output lines will be preceded by < or >, depending on which file the respective line was missing in. If you want to remove those indicators as well, use something like this:
for /f "tokens=1*" %a in (
'diff file1.txt file2.txt ^| findstr /r /c:"^<" /c:"^>"'
) do #echo %b >>file3.txt
Change %a and %b into %%a and %%b if you want to run this in a batch file.
FINDSTR /v /b /e /l /g:file2. file1. >file3.
should produce the required results - lines in file1 missing from file2.
/v says 'not found', /b /e forces exact match - not part-of-line-matches /l literal.

Batch file to remove first 10 chars from txt file

I'm trying to remove the first 10 characters from multiple lines inside a text file using a batch script, then output the results to a new file. I ran across this and it got me pointed in the right direction but the final output isn't working.
Here's what I've got so far:
setLocal EnableDelayedExpansion
CSCRIPT /nologo %windir%\System32\prnport.vbs -l > c:\IPPorts.txt
type c:\IPPorts.txt | findstr IP_ > c:\IPPorts2.txt
for /f "tokens=*" %%a in (c:\IPPorts2.txt) do (set line=%%a set chars=!line:~10! > c:\IPPorts3.txt)
for /f "delims=" %%x in (c:\IPPorts3.txt) do CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%x
The 2nd line exports a list of printer ports to a file named IPPorts.txt. The 3rd finds the lines with "IP_" in them and exports to IPPorts2.txt. The 4th line is supposed to remove unneeded text (which it isn't doing) and export to IPPorts3.txt. And the last line will take the results from IPPorts3.txt and then delete those ports.
IPPorts.txt is as follows:
Server name
Port name IP_172.20.51.11
Host address 172.20.51.11
Protocol RAW
Port number 9100
SNMP Disabled
These lines are repeated for every port, of which there are several. Since I only need the line containing the port name, IPPorts2.txt looks like this:
Port name IP_172.20.51.11
Port name IP_172.20.52.58
Port name IP_172.20.53.16
Port name IP_172.20.54.19
Port name IP_172.20.55.15-1
Port name IP_172.20.55.15
Port name IP_172.20.55.11
Where I'm having trouble is removing the "Port name " portion of the lines (the first 10 characters). I want the output to read on each line as "IP_X.X.X.X". The problem is the 3rd file is always empty.
Where am I going wrong? Any help is greatly appreciated.
EDIT:
This is further down under Endoro's answer, but I thought it might be nice to post the answer here. Here's what I changed the 4th line to:
for /f "tokens=* delims=" %%c in ('type c:\IPPorts2.txt') do (
set LINE=%%c
>> c:\IPPorts3.txt echo !LINE:~10!
)
This has corrected my problems. Thanks everyone!
try this:
(for /f "tokens=3" %%i in (IPPorts2.txt) do #echo %%i)>IPPorts3.txt
Script to get directory name out of DIR command output :
...
20/09/2014 01:23 [DIR] some1
21/09/2014 02:34 [DIR] some2
22/09/2014 03:45 [DIR] some3
23/09/2014 11:22 [DIR] some4
...
We want it to be:
some1
some2
some3
some4
...
Code :
#FOR /f "tokens=4" %%D IN (i:\test.txt) DO #( echo %%D ) >> result.txt
In your case tokens=3, not perfect but does the job with few lines manually edited in the result.
(For /f "tokens=3delims= " %%i in (ipports2.txt) do echo %%i) >ipports3.txt
should do it for you.
The paretheses are important - ensure that the file is created anew. If omitted, will only generate the last line.
Simply uses the delimiter [space] to tokenise the string on each line into token1=Port, token2=Name and sets %%i to each token3 in turn.
The following isn't really a different solution but merely a suggestion to simplify your script by reducing the number of output files.
In fact, it is possible to exclude all of them from the script, unless you need to keep them for history.
Basically, the idea is first to apply FINDSTR directly to the output of prnport.vbs:
CSCRIPT /nologo %windir%\System32\prnport.vbs -l | FINDSTR "IP_"
then apply a loop directly to the output of FINDSTR (note the single quotation marks around the piped command line, as well as the escaped |):
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO …
and call prnport.vbs with another set of arguments in that same loop:
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO (
CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%A
)
The tokens option of a FOR /F loop specifies which token (or field) to take based on a specific delimiter or set of delimiters. The default set of delimiters is a space, a comma, a tab. Your Port name IP_whatever lines conveniently consist of exactly three tokens and the third one is what you are after, hence "tokens=3" in the options.
So, as you can see, no output files, the necessary value is extracted and passed to the target command in the same iteration.

batch file to read specific letters from a text file?

I have a txt file called named.txt Is there a way to create a batch file to read every letters 2 through 7 letters from every line from name.txt and ignore the rest and output it onto a different txt file called name2.txt. For example I have this in the txt file:
G2010060sample.png
G2010061sample.png
G2010062sample.png
G2010063sample.png
and the batch file would create a new txt file like this :
2010060.png
2010061.png
2010062.png
2010063.png
Excellent online ressource http://ss64.com/nt/syntax.html
#echo off
if exist output.txt del output.txt
for /f "delims=" %%i in (input.txt) do call :ParseLine %%i
goto :eof
:ParseLine
set line=%1
set line=%line:~1,7%
echo %line%.png>> output.txt
goto :eof
cut -b 2-7,15-18 < infile.txt > outfile.txt

Resources