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
Solved

how to deal with big text files.

Posted on 2002-06-12
7
600 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
perl script help 12 105
unable to remove invisible characters with perl 2 89
File Find regex problem 4 64
create a gui in perl 3 97
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

808 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