473,698 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove duplicates from file

2 New Member
hello all

excuse my english, I`m a beginer in java and I want to remove duplicate cities + rearrenging file`s lines elements from one 150 mb file, in my file I have that:

"68586240","685 86367","IRVING"
"68586368","685 86431","DENTON"
"68586432","685 86495","DENTON"

and I want to become

"68586240","685 86367","IRVING"
"68586368","685 86495","DENTON"

i mean

1 2 x
3 4 x
5 6 y

become

1 4 x
5 6 y

I have allready done the program but i`m stuck . can anyone help me?

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.regex.*;
  3. import java.io.*;
  4.  
  5.     public class test {
  6.  
  7.         private static final String REGEX = ",";
  8.         private static Pattern pattern;
  9.  
  10.          public static void main(String[] argv) {
  11.  
  12.              pattern = Pattern.compile(REGEX);
  13.  
  14.             try{
  15.                 FileInputStream fstream = new FileInputStream("test.txt");
  16.                 DataInputStream in = new DataInputStream(fstream);
  17.                 BufferedReader br = new BufferedReader(new InputStreamReader(in));
  18.                 String strLine;
  19.  
  20.                 int i = 1;
  21.  
  22.  
  23.                 BufferedWriter make_file = new BufferedWriter(new FileWriter("file_temp.txt", true));
  24.                 String make_test = "\"TEST\",\"TEST\",\"TEST\"";
  25.                 make_file.write(make_test);
  26.                 make_file.close();
  27.  
  28.  
  29.                 BufferedWriter out = new BufferedWriter(new FileWriter("good.txt", true));                
  30.  
  31.                 while ((strLine = br.readLine()) != null)   {
  32.  
  33.                     //explode line                    
  34.                     String[] items = pattern.split(strLine);
  35.  
  36.                     // read + memory content from temp file    
  37.                     FileReader input = new FileReader("file_temp.txt");
  38.                     BufferedReader bufRead = new BufferedReader(input);
  39.                        String line = ""; 
  40.                     String temp = "";    
  41.                     line = bufRead.readLine();
  42.                     while (line != null){
  43.                         temp = temp+line;
  44.                         line = bufRead.readLine();                
  45.                     }
  46.                     bufRead.close();
  47.  
  48.                     // explode line from temp file
  49.                     String[] items_temp = pattern.split(temp);
  50.  
  51.                     //delete temp file
  52.                     new    File("file_temp.txt").delete();
  53.  
  54.                     BufferedWriter out_temp = new BufferedWriter(new FileWriter("file_temp.txt", true));
  55.                     out_temp.write(strLine);
  56.                     out_temp.close();
  57.  
  58.                     String x1 = items_temp[2];
  59.                     String x2 = items[2];
  60.  
  61.                     if (x1.equalsIgnoreCase(x2)) {
  62.                         // ??????????
  63.                     } 
  64.                     else {
  65.                         out.write(items[0]+","+items_temp[1]+","+items[2]+"\n");
  66.                         /* 
  67.                          input   output  correct 
  68.                          1 2 x   1 2 x   1 4 x
  69.                          3 4 x   5 6 y   5 6 y
  70.                          5 6 y
  71.                         */
  72.  
  73.                     }
  74.  
  75.                     System.out.println(i+"-"+strLine);            
  76.                     i++;
  77.  
  78.                 }
  79.                  out.close();
  80.                 in.close();
  81.             }
  82.             catch (Exception e){
  83.                 System.err.println("Error: " + e.getMessage());
  84.             }    
  85.  
  86.     }
  87.  
  88. }
  89.  
  90.  
tnx
Oct 4 '07 #1
6 10304
dmjpro
2,476 Top Contributor
Sorry for less time in my hand to look after your code throughly :-)
Have a look at my code ....

Expand|Select|Wrap|Line Numbers
  1. BufferedReader in = new BufferedReader(new FileReader("src_file"));
  2. FileWriter out = new FileWriter("tgt_file");
  3. String line;
  4. HashSet hs = new HashSet();
  5. String op = "";
  6. while(!(line=in.readLine())!=null){
  7. StringTokenizer s = new StringTokenizer(line,",");
  8. String city;
  9. while(s.hasMoreTokes()) city = s.nextToken();
  10. op += hs.add(city) ? (line+System.getProperty("line.separator")) : "";
  11. //here it ignores duplicate items
  12. }
  13. out.write(op);
  14. in.close();
  15. op.close();
  16.  
Enjoy this code.
Good Luck :-)

Kind regards,
Dmjpro.
Oct 4 '07 #2
r035198x
13,262 MVP
Sorry for less time in my hand to look after your code throughly :-)
Have a look at my code ....

Expand|Select|Wrap|Line Numbers
  1. BufferedReader in = new BufferedReader(new FileReader("src_file"));
  2. FileWriter out = new FileWriter("tgt_file");
  3. String line;
  4. HashSet hs = new HashSet();
  5. String op = "";
  6. while(!(line=in.readLine())!=null){
  7. StringTokenizer s = new StringTokenizer(line,",");
  8. String city;
  9. while(s.hasMoreTokes()) city = s.nextToken();
  10. op += hs.add(city) ? (line+System.getProperty("line.separator")) : "";
  11. //here it ignores duplicate items
  12. }
  13. out.write(op);
  14. in.close();
  15. op.close();
  16.  
Enjoy this code.
Good Luck :-)

Kind regards,
Dmjpro.
@OP Do not use InputStreams to read text files. Use FileReader or Scanner
@dmjpro Do not use StringTokenizer
Oct 4 '07 #3
JosAH
11,448 Recognized Expert MVP
@OP: what should be done with the following file contents:

Expand|Select|Wrap|Line Numbers
  1. 1 2 x
  2. 3 4 x
  3. 5 6 x
  4.  
or with this?

Expand|Select|Wrap|Line Numbers
  1. 1 2 x
  2. 3 4 y
  3. 5 6 x
  4.  
I think the problem description should be less ambiguous and more complete
to start with before we can start thinking of a solution.

kind regards,

Jos
Oct 4 '07 #4
topala1984
2 New Member
more info

input :
1 2 x
3 4 x
5 6 y

output :
1 2 x
5 6 y

correct:
1 4 x
5 6 y

compare the third element of line 1 with the third element of line 2.
if equal join first element from line 1 with second element from line 2 and the third comun element in ... all in new file
if not equal put line 2 in new file

compare the third element of line 2 with the third element of line 3.
if equal join first element from line 2 with second element from line 3 and the third comun element ... all in new file
if not equal put line 3 in new file
............... ..............

compare the third element of line line n-1 with the third element of line n.
if equal join first element from line n-1 with second element from line n and the third comun element ... all in new file
if not equal put line n in new file

tnx all
Oct 4 '07 #5
dmjpro
2,476 Top Contributor
more info

input :
1 2 x
3 4 x
5 6 y

output :
1 2 x
5 6 y

correct:
1 4 x
5 6 y

compare the third element of line 1 with the third element of line 2.
if equal join first element from line 1 with second element from line 2 and the third comun element in ... all in new file
if not equal put line 2 in new file

compare the third element of line 2 with the third element of line 3.
if equal join first element from line 2 with second element from line 3 and the third comun element ... all in new file
if not equal put line 3 in new file
............... ..............

compare the third element of line line n-1 with the third element of line n.
if equal join first element from line n-1 with second element from line n and the third comun element ... all in new file
if not equal put line n in new file

tnx all

Did you try my code :-)

Debasis Jana
Oct 5 '07 #6
JosAH
11,448 Recognized Expert MVP
Did you try my code :-)

Debasis Jana
I noticed that you're spoonfeeding quite a bit of (incorrect) code lately; please don't do that.

Jos
Oct 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7953
by: Wm | last post by:
I have a table of users in mySQL that appears to have a lot of duplicates. What's the best way to look at the userID and email and delete the duplicates? Thanx, Wm
3
8644
by: m|sf|t | last post by:
All, Is it possible to use PHP to open/read a TXT file (i.e. IP.TXT) that contains ip addresses (1 per line), remove any duplicates found, and re-write the file back out to IP_NEW.TXT ? The reason is, I have ips coming from several sources, i.e. URLSCAN logs, MySQL dbs, etc. and I can use a DISTINCT on the individual queries, but I then combine all the results into 1 file which may contain duplicates. Thanks.
6
2398
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec Qty Invoice# Item Supplier Status POReceivedDate 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
11
5831
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am using the split function with ' ', but how do i get it to take into account punctuation marks such as ',',',' etc? Also I am then trying to add contents of an array of strings to an arraylist, but only if the string isn't already there. I was using...
14
21965
by: BarrySDCA | last post by:
I have a database being populated by hits to a program on a server. The problem is each client connection may require a few hits in a 1-2 second time frame. This is resulting in multiple database entries - all exactly the same, except the event_id field, which is auto-numbered. I need a way to query the record w/out duplicates. That is, any records exactly the same except event_id should only return one record. Is this possible??
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.