Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to deal with big text files.

Posted on 2002-06-12
7
Medium Priority
?
625 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 400 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
Technology Partners: 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

604 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