Solved

how to deal with big text files.

Posted on 2002-06-12
7
604 Views
Last Modified: 2012-06-21
The problem:
- We have a big text files.
- a text file of bad words

The result:
I'd like to have a new text files without all bad words

any ideas ???
0
Comment
Question by:us111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:johnsm
ID: 7073125
Read in the file, line by line. As you read in each line, check it for "bad" words. If there are any, then eliminate them and write the data back to a new file with the bad words having been removed.

What are you calling bad words? Curse words, or words that are improperly spelled or formatted?
0
 
LVL 8

Author Comment

by:us111
ID: 7073320
- please give me a working example

- bad words: I mean words like 'a' , 'the'....
0
 
LVL 1

Accepted Solution

by:
johnsm earned 100 total points
ID: 7073592
# Open the data file for reading
# ------------------------------
  open (FILE, "$dirpath\\$file");

# Open a second file for writing
# ------------------------------
  open (FILE2, ">$dirpath\\$file");

# Read through it one line at a time
# ----------------------------------
  while ($dataline = <FILE>) {

# Replace the bad words with nothing
# ----------------------------------
  $dataline =~ s/a//ig;
  $dataline =~ s/the//ig;

# or do it all on one line
# ------------------------
  $dataline =~ s/(a|the|whatever|whatever)//ig;

# Write the line back to the new file
# -----------------------------------
  print FILE2 "$dataline\n";

# End your loop
# -------------
  }

# Close both files
# ----------------
  close(FILE);
  close(FILE2);

# The end
# -------
  end;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:johnsm
ID: 7073596
You probably won't need the \n on the line

print FILE2 "$dataline\n";

The line should already contain a new line character. So maybe just

print FILE2 "$dataline";

will work for you.


0
 
LVL 1

Expert Comment

by:johnsm
ID: 7073603
oops. One other mistake I now see you can't open the same file for both reading and writing.

Change

# Open a second file for writing
# ------------------------------
 open (FILE2, ">$dirpath\\$file");

to

# Open a second file for writing
# ------------------------------
 open (FILE2, ">$dirpath\\$file2");

sorry.


0
 
LVL 1

Expert Comment

by:smisk
ID: 7073971
I think you're going to want to put word boundaries on that, otherwise all occurences of "a" will be removed (ie, "that" will become "tht").

Change the replace lines to the following format :

s/(\W|^)a(\W|$)/ /g;

where "a" is what you want to remove.
0
 
LVL 84

Expert Comment

by:ozo
ID: 7074630
#or
s/\b(a|the|whatever|whatever)\b//ig;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remove duplicates from the csv file 13 118
Having a problem with ActiveState Perl on 64-bit Windows 7 3 77
Create an automated page index 9 82
Perl Versus AWK? 7 71
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question