# Compare Csv file

**URL:** <https://forum.katalon.com/t/compare-csv-file/35956>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 7, 2019, 10:32am UTC](https://forum.katalon.com/t/compare-csv-file/35956 "2019-11-07T10:32:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel8](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@daniel8](https://forum.katalon.com/u/daniel8)\
**Post date:** [November 7, 2019, 10:32am UTC](https://forum.katalon.com/t/compare-csv-file/35956/1 "2019-11-07T10:32:55Z")

</div>

Hi, I would like to compare two CSV Files and write the differences to a third CSV file. I have tried using tokenize but it is deprecated.

---

<div class="post-metadata">

**Author:** ![erikpatar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/erikpatar/32/23869_2.png) [@erikpatar](https://forum.katalon.com/u/erikpatar)\
**Post date:** [November 7, 2019, 12:00pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/2 "2019-11-07T12:00:22Z")

</div>

same, do you have an idea?

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [November 7, 2019, 3:11pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/3 "2019-11-07T15:11:35Z")

</div>

Diff programming tends to involve significant amount of efforts. It may look easy at first, but soon turns out bloody.

I would rather recommend you to use diff-dedicated tools. For example WinMerge:

- [https://winmerge.org/?lang=en](https://winmerge.org/?lang=en)

You can execute any exe from Groovy script (= Katalon Test Case script) using ProcessBuilder.  
See the following article.

> **[Calling Other Processes From Groovy (Example)](https://coderwall.com/p/nswp1q/calling-other-processes-from-groovy)**
>
> A protip by tessthyer about groovy.

WinMerge has Command Line Interface

[https://manual.winmerge.org/en/Version\_control.html](https://manual.winmerge.org/en/Version_control.html)

So it would be a fun to launch WinMerge from Katalon Studio.

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [November 7, 2019, 3:46pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/4 "2019-11-07T15:46:30Z")

</div>

hi,

simple demo where used PDFUtil lib and apache pdfBox libs

download pdfUtil.jar from  
[http://www.testautomationguru.com/introducing-pdfutil-to-compare-pdf-files-extract-resources/](http://www.testautomationguru.com/introducing-pdfutil-to-compare-pdf-files-extract-resources/)  
and add it to the project Drivers folder  
restart KS

This is first demo pdf page file1.pdf  
human  
cat  
dog  
snake  
worm  
eagle  
rabbit

This is second demo pdf page file2.pdf  
human  
cat  
dog  
squirrel  
worm  
eagle  
rabbit

and the third file file3.pdf have differences  
pages have differences file1 have words [first, snake] which are not in file2, file2 instead have [second, squirrel]

TESTCASE  
import com.testautomationguru.utility.PDFUtil;  
PDFUtil pdfUtil = new PDFUtil();  
// returns the pdf content from page number 1  
String file\_1 = pdfUtil.getText(“path where your pdf lies/file1.pdf”,1);  
String file\_2 = pdfUtil.getText(“path where your pdf lies/file2.pdf”,1);

def diff1 = CustomKeywords.‘readPdfFile.verifyPdfContent.findNotMatching’(file\_1, file\_2)  
def diff2 = CustomKeywords.‘readPdfFile.verifyPdfContent.findNotMatching’(file\_2, file\_1)

String message = "pages have differences file1 have words "+diff1+ " which are not in file2, file2 instead have "+diff2

println "pages have differences file1 have words "+diff1+ " which are not in file2, file2 instead have "+diff2

KEYWORDS  
import org.apache.pdfbox.pdmodel.PDDocument  
import org.apache.pdfbox.pdmodel.PDPage  
import org.apache.pdfbox.pdmodel.PDPageContentStream  
import org.apache.pdfbox.pdmodel.font.PDFont  
import org.apache.pdfbox.pdmodel.font.PDType1Font  
import org.apache.pdfbox.text.PDFTextStripper  
@Keyword  
public List findNotMatching(String sourceStr, String anotherStr){  
StringTokenizer at = new StringTokenizer(sourceStr, " ");  
StringTokenizer bt = null;  
int i = 0, token\_count = 0;  
String token = null;  
boolean flag = false;  
List missingWords = new ArrayList();  
while (at.hasMoreTokens()) {  
token = at.nextToken();  
bt = new StringTokenizer(anotherStr, " ");  
token\_count = bt.countTokens();  
while (i \< token\_count) {  
String s = bt.nextToken();  
if (token.equals(s)) {  
flag = true;  
break;  
} else {  
flag = false;  
}  
i++;  
}  
i = 0;  
if (flag == false)  
missingWords.add(token);  
}  
return missingWords;  
}

```
public void writeDfferences(String message, String filename){
	
	PDDocument doc = new PDDocument();
	try {
		PDPage page = new PDPage();
		doc.addPage(page);
		
		PDFont font = PDType1Font.TIMES_ROMAN;

		PDPageContentStream contents = new PDPageContentStream(doc, page);
		contents.beginText();
		contents.setFont(font, 12);
		contents.newLineAtOffset(50, 700);
		contents.showText(message);
		contents.endText();
		contents.close();
		
		doc.save(filename);
	}
	finally {
		doc.close();
	}
}

```

CustomKeywords.‘readPdfFile.verifyPdfContent.writeDfferences’(message, “path where your pdf lies/file3.pdf”)

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [November 7, 2019, 3:50pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/5 "2019-11-07T15:50:21Z")

</div>

hello,

sorry, read again this subject, and question was CSV not pdf, will try to do for csv same 🙂

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [November 9, 2019, 7:41pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/6 "2019-11-09T19:41:51Z")

</div>

hi,

ok, i got it something, works but not so pretty 🙂

```
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

String originalPath = System.getProperty("user.dir")+"\\ExcelFiles\\original.csv";
String comparePath = System.getProperty("user.dir")+"\\ExcelFiles\\compare.csv";
String differencesPath = System.getProperty("user.dir")+"\\ExcelFiles\\differences.csv";

//createCSVFile();
int[] rowsCols = getRowsColsNo(originalPath);
int[] rowsCols2 = getRowsColsNo(comparePath);
System.out.println(rowsCols[0]); //count of rows
System.out.println(rowsCols[1]); //count of columns

File file = new File(differencesPath);
if(file.delete())
{
	System.out.println("File deleted successfully");
}
else
{
	System.out.println("Failed to delete the file");
}
createCSVFile(differencesPath);//create new file

String mess = "original.csv does not match with compare.csv ";
//start from row 0 (header)
writeToDiffFile(differencesPath, null, mess, 0, 0);//write header wrote
String[] strDiff = new String[rowsCols[0]];
for (int i = 0; i < rowsCols[0]; i++) {
	String line = Files.readAllLines(Paths.get(originalPath)).get(i);
	String line2 = Files.readAllLines(Paths.get(comparePath)).get(i);
	String diff = getDiff(line, line2);
	strDiff[0] = diff;
	strDiff[1] = "in row "+i;
	writeToDiffFile(differencesPath, strDiff, null, 1, i);
}

public static int[] getRowsColsNo(String fileName) {
	Scanner scanIn = null;
	int rows = 0;
	int cols = 0;
	String InputLine = "";
	try {
		scanIn = new Scanner(new BufferedReader(
				new FileReader(fileName)));
		scanIn.useDelimiter(",");
		while (scanIn.hasNextLine()) {
			InputLine = scanIn.nextLine();
			String[] InArray = InputLine.split(",");
			rows++;
			cols = InArray.length;
		}

	} catch (Exception e) {
		System.out.println(e);
	}
	//scanIn.close();
	return [rows, cols] ;
}

public static void createCSVFile(String fileName) throws IOException {
	//String[] HEADERS = { "author", "title","year"};
	CSVPrinter printer = null;
	FileWriter out = new FileWriter(fileName);
	try {
		printer = new CSVPrinter(out, CSVFormat.DEFAULT);
		// .withHeader(HEADERS));
		printer.printRecord();
	} catch (IOException e) {
		e.printStackTrace();
	}
	printer.flush();
	out.close();
}

public static void writeToDiffFile(String fileName, String[] values, String val, int counter, int rowNum) throws IOException {
	CSVPrinter csvPrinter;
	//FileWriter out = new FileWriter(fileName);
	Writer writer = Files.newBufferedWriter(Paths.get(fileName), StandardOpenOption.APPEND);
	//BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), StandardOpenOption.APPEND);
	if(counter == 1) {
		csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
		 // .withHeader("author", "title", "year"));
		csvPrinter.printRecord(Arrays.asList(values));
	}
	else{
		csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
		csvPrinter.printRecord(val);
	}
	csvPrinter.flush();
	writer.close();
}

public static String getDiff(String str1, String str2){
	String[] strList1 = str1.split(",");
	String[] strList2 = str2.split(",");

	List<String> list1 = Arrays.asList(strList1);
	List<String> list2 = Arrays.asList(strList2);

	// Prepare a union
	List<String> union = new ArrayList<>(list1);
	union.addAll(list2);

	// Prepare an intersection
	List<String> intersection = new ArrayList<>(list1);
	intersection.retainAll(list2);

	// Subtract the intersection from the union
	union.removeAll(intersection);
	List<String> arr = new ArrayList<>();
	for (String s : union) {
		arr.add(s);
	}
	return String.join(", ", arr);
}

```

original.csv  
“author”,“title”,“year”  
“Dan Simmons”,“Hyperion”,2019  
“Douglas Adams”,“The Hitchhiker’s Guide to the Galaxy”,2020  
“Mat Pat”,“Java Book”,2018  
“Tim Given”,“Maths”,2015

compare.csv  
“author”,“title”,“year”  
“Dan Simmons”,“Hyperion”,  
,“The Hitchhiker’s Guide to the Galaxy”,2020  
“Mat Pat”,“Java Book”,100  
“Tim Given”,

differences.csv

“original.csv does not match with compare.csv "  
“”,in row 0,  
2019,in row 1,  
“”“Douglas Adams””, “,in row 2,  
“2018, 100”,in row 3,  
“”“Maths””, 2015",in row 4,

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 4, 2020, 12:36pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/7 "2020-08-04T12:36:19Z")

</div>

Hi @Timo_Kuisma1,  
I’m trying to use this code for comparing CSV files and writing the differences, But when I ran the test case it’s getting fail could you please help to figure out the issue of this.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/6/6bff4a528a0e760e49c752eb559f4f993331ab59.png)

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [August 4, 2020, 2:49pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/8 "2020-08-04T14:49:13Z")

</div>

hi,

do you have import these classes

import org.apache.commons.csv.CSVFormat;  
import org.apache.commons.csv.CSVPrinter;  
import org.apache.commons.csv.CSVRecord;

if not  
then download .jar from

[https://commons.apache.org/proper/commons-csv/download\_csv.cgi](https://commons.apache.org/proper/commons-csv/download_csv.cgi)

and add it to the Drivers folder

---

<div class="post-metadata">

**Author:** ![ratnadeeph](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@ratnadeeph](https://forum.katalon.com/u/ratnadeeph)\
**Post date:** [August 7, 2020, 7:17pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/9 "2020-08-07T19:17:22Z")

</div>

@kazurayam, Is it possible to launch WinMerge from Katalo studio? I need to do exactly this for my testing needs. Appreciate your help.

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [August 7, 2020, 8:18pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/10 "2020-08-07T20:18:24Z")

</div>

Hi,

which files you should merge with WinMerge, maybe you can use java code to merge files

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 7, 2020, 11:39pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/11 "2020-08-07T23:39:25Z")

</div>

Following post shows how to execute an exe from Katalon Test Case. The following case takes “ipconfig” command, but essentially the same method applies for WinMerge CLI.

> [@"Interactive" Test with Katalon](http://forum.katalon.com/t/interactive-test-with-katalon/18215/6):
>
> Let me show you an example. Please make a test case with any name in Katalon Studio, paste the following code, and RUN it. This script will execute Windows’ ipconfig command, read the stdout and stderr from the process and print it. import java.nio.charset.Charset String[] CMD = { "cmd", "/c", "ipconfig.exe"} String WINCHARSET = "MS932" // Charset used for the output of the command, depends on your Windows PC environment, // see https://docs.oracle.com/javase/8/docs/technotes/guides/intl…

The WinMerge doc tells how to execute it in commandline

- [Using WinMerge with other tools - WinMerge 2.16 Manual](https://manual.winmerge.org/en/Version_control.html#:~:text=The%20WinMerge%20command%2Dline%20interface,to%20launch%20a%20WinMerge%20operation).

- [Command line - WinMerge 2.16 Manual](https://manual.winmerge.org/en/Command_line.html)

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 12, 2020, 1:31pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/12 "2020-08-12T13:31:31Z")

</div>

hi @Timo_Kuisma1,  
Thanks for your response, I tried as you suggested but still no luck getting the same error.  
Below are the attachments could you pls verify and correct me.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/3/3fe3de88cff1e47dd837fe8c52c2bb661e70083d.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/5/5fbd5ac2a35e1b08a7713df51f8584365ea9c455.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/f/f054382153955587196022913a04532cfeebcb33.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/5/58649c7a527030dac8849a0c2ad98a46e004ff30.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/d/d42b5782028db01c8291c9cfaba38b856423eede.png)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 12, 2020, 2:07pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/13 "2020-08-12T14:07:21Z")

</div>

@bhargavia

You put the jar into the `KatalonInstallationFolder\configuration\resources\drivers` folder. That is not the appropriate place.

You should put the jar into the `<yourProjectFolder>\Drivers` folder. And you should stop/restart Katalon Studio to let it acknowledge the new jar in the Drivers folder.

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 12, 2020, 2:10pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/14 "2020-08-12T14:10:52Z")

</div>

Thank You @kazurayam i will change the path and i will try.

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 12, 2020, 3:32pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/15 "2020-08-12T15:32:53Z")

</div>

hi @kazurayam i changed the path even through getting the same error.

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [August 12, 2020, 7:27pm UTC](https://forum.katalon.com/t/compare-csv-file/35956/16 "2020-08-12T19:27:25Z")

</div>

Hi,

do as Kazu said, restart Katalon studio to get .jar files into the classpath

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 13, 2020, 1:13am UTC](https://forum.katalon.com/t/compare-csv-file/35956/17 "2020-08-13T01:13:00Z")

</div>

> [@bhargavia](#):
>
> i changed the path even through getting the same error.

Could you take a screenshot of the `<yourProjectDir>\Drivers` folder and share it?

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 13, 2020, 2:57am UTC](https://forum.katalon.com/t/compare-csv-file/35956/18 "2020-08-13T02:57:34Z")

</div>

hi @kazurayam below is the screenshot of project drivers folder.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/2/263fe5392dbc2572dab56580395137b053ad663f.png)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 13, 2020, 3:10am UTC](https://forum.katalon.com/t/compare-csv-file/35956/19 "2020-08-13T03:10:32Z")

</div>

This looks strange.

Could you take a screenshot of the `<yourProjectDir>`?

---

<div class="post-metadata">

**Author:** ![bhargavia](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhargavia/32/16103_2.png) [@bhargavia](https://forum.katalon.com/u/bhargavia)\
**Post date:** [August 13, 2020, 3:14am UTC](https://forum.katalon.com/t/compare-csv-file/35956/20 "2020-08-13T03:14:39Z")

</div>

sure,this is my project directory

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/1/1e2f338f52c97c4df6e0ddd68709ba579c8759d9.png)

[Next page](https://forum.katalon.com/t/compare-csv-file/35956.md?page=2)
