How to write a perl script to search text files for a specific string and copy the whole line to a new file?

I have several very large text files (over 1 gb total) that I want to strip out any line that has a specific string, and place that entire line in a text file. Best case it would open every file in the directory. Thanks for any pointers (existing websites?)


Related Blogs

Comments

  1. Michael JB says:

    a quicker way than using Perl would be to use grep…if you have access to it. It will be on your system if you are using Linux or Unix and is available in Cygwin if you are using windows. Try this:

    grep -h YOUR_KEYWORD_HERE *.txt > results.txt

    if you MUST use Perl, the absolute best programming language in the world IMHO, try this:

    ——–cut——–
    # this is so we can use English shortcuts like $ARG and such
    use English;

    # open a file to store the results
    open (OUTPUT, ">results.txt") || die ("Could not open file results.txt; $OS_ERROR");

    # read each input file from the command line
    # i.e. perl keyword.pl file1.txt file2.txt …
    while (<>) {

    # if the line has the keyword, print it to the output file
    if ($ARG =~ /YOUR_KEYWORD_HERE/) {
    print OUTPUT $ARG;
    warn "Found YOUR_KEYWORD_HERE in $ARGV\n";
    }
    }

    # close the output file
    close (OUTPUT);
    ——–cut——–

    I’ll leave it as an exercise for you to figure out which one is faster when dealing with files larger than 1 MB or so. Also, you might want to do both and then diff the results to see any differences between the two.

legitimate work from home jobs | Event Planning Bay Area | rent video games | Homeloans | Plumbing Courses in Manchester | increase fertility | Green Drink | Photo Keyring | Vitamin D Deficiency Symptoms | skip bags | Order Fulfillment Software | virtual receptionist | Educators Credit | bodybuilding workouts | window cleaning
payday loan payday loans fibromyalgia ezetimibe buy ziagen buy cipro newyork travel narrow angle glaucoma headache Poker Software sciatic nerve damage cluster headache sciatica glaucoma fibromyalgia syndrome