Solved

how to deal with big text files.

Posted on 2002-06-12
7
587 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
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now