473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

remove lines from a huge text file

89
Hi,
I have a 10 million record in a tab delimited text file (data.txt) and I have another file with line numbers (line_num.txt).
My data.txt is as follows
Expand|Select|Wrap|Line Numbers
  1. first 1  res  234
  2. sec   23 fre  890
  3. dec   26 der  690
  4. ...
  5.  
My line_num.txt is as follows:
Expand|Select|Wrap|Line Numbers
  1. 23
  2. 456
  3. 3467
  4. ...
  5.  
I have to remove the lines ( 23rd, 456th, 3467th) line from data.txt. How do I do it at ease.

What I have in my mind is to load these line number data ( 23rd, 456th, 3467th) into a hash (query hash) and go through the data.txt line by line and if the line appears in line_number_data_hash, then remove those lines. I don't have any code ready but I would like to whether there are any other way of doing this bit faster as I have millions of records to clean up.
Please let me know
Expand|Select|Wrap|Line Numbers
  1. 1. Load line_num into line_num_hash
  2. 2. count=0;
  3. 3.  every line in a text file (<>) {
  4.    if ($count exists in line_num_hash) {
  5.      load the line into data_query_hash
  6.      count++;
  7.    }
  8.  
  9. 4. open data.txt and iterate through every line (<>) {
  10.    if ($_ exits in data_query_hash ) {
  11.     #don't print (that is to be removed)
  12.     }  else { print the line  }
  13.  
  14. }
  15.  
  16.  
Any other way of doing this?
Aug 29 '10 #1
2 2803
RonB
589 Expert Mod 512MB
The approach that you're considering is what I'd do and is the most efficient that I can think of, assuming that you don't throw in unnecessary logic.

Try writing the script and when you get stuck, post the code and include any/all warnings and errors that it produces.
Aug 29 '10 #2
RonB
589 Expert Mod 512MB
I was bored so I wrote it for you.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. @ARGV = 'data.txt';
  7. $^I = '.bak';
  8.  
  9. my $num_file = 'line_num.txt';
  10. my %line_number;
  11.  
  12. open my $num_fh, '<', $num_file or die "Can't open '$num_file $!";
  13. while ( my $line = <$num_fh> ) {
  14.     chomp $line;
  15.     $line_number{$line}++;
  16. }
  17.  
  18. while( <> ) {
  19.     next if exists $line_number{$.};
  20.     print;
  21. }
Aug 29 '10 #3

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

Similar topics

3
by: sahm | last post by:
does anyone know how to do it..i need help
5
by: WoodenSword | last post by:
Hi, I am trying to read a huge text file (2GB) using StreamReader. I do this: Read a line (readline()) I process the line a bit (split it with delimeter and change fields a bit) execute...
10
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter...
15
by: batman | last post by:
i have a text file that is like: date = OCT0606 asdf sdaf asdfasdgsdgh asdfsdfasdg START-OF-DATA asdfasdfg asdfgdfgsfg
15
by: d4 | last post by:
I have a vbscript (below) I want to rewrite in C# but I cannot get it to work (unless there is a better way to do it). The script will combine 2 lines if the (next) line contains a "+",...
2
by: cdun2 | last post by:
Hello, I have some code that reads each line of a text file, and if a line is found where the length of the string in the line is 384, it writes the line to a text file. The other step that I...
6
by: EricR | last post by:
I am trying to bcp import a text file into a SQL Server 2000 database. The text file is coming out of a java application where order information is written to the text file. Each record is on it's...
15
by: Fab108 | last post by:
Hello everyone, i am new here. Hmm...can you all please help me with this..what is the code for reading lines from a text file..Thanks =)
6
by: tolkien | last post by:
Hi,i have this problem.I'm reading strings from a text file and i want to know in which line of the file i am. What i did is this: char string; while(!feof(text_file)){...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.