Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Concatenating selective lines from a bunch of files into a singlefile

Reply
Thread Tools

Concatenating selective lines from a bunch of files into a singlefile

 
 
Rider
Guest
Posts: n/a
 
      07-25-2009
Hi experts,

I have a unix dir, test and it has 100 files. I need to extract all
the lines that contain a word "keyword" key word" (case insensitive)
from each of those files and put all those lines into another file,
concatenate.txt. Similarly, I want all the lines that don't contain
that key word (keyword or key word) into another file,
non_concatenate.txt.

I know a little bit of perl and I am wondering how can I do it here.

Thanks,
J
 
Reply With Quote
 
 
 
 
Rider
Guest
Posts: n/a
 
      07-25-2009
On Jul 25, 9:10*am, Rider <(E-Mail Removed)> wrote:
> Hi experts,
>
> I have a unix dir, test and it has 100 files. I need to extract all
> the lines that contain a word "keyword" key word" (case insensitive)
> from each of those files and put all those lines into another file,
> concatenate.txt. Similarly, I want all the lines that don't contain
> that key word (keyword or key word) into another file,
> non_concatenate.txt.
>
> I know a little bit of perl and I am wondering how can I do it here.
>
> Thanks,
> J


What is the easiest way? To use File::Find module and then use grep,
join commands?

Thanks in advance,
J
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      07-25-2009
In article
<(E-Mail Removed)>,
Rider <(E-Mail Removed)> wrote:

> On Jul 25, 9:10*am, Rider <(E-Mail Removed)> wrote:
> > Hi experts,
> >
> > I have a unix dir, test and it has 100 files. I need to extract all
> > the lines that contain a word "keyword" key word" (case insensitive)
> > from each of those files and put all those lines into another file,
> > concatenate.txt. Similarly, I want all the lines that don't contain
> > that key word (keyword or key word) into another file,
> > non_concatenate.txt.
> >
> > I know a little bit of perl and I am wondering how can I do it here.
> >
> > Thanks,
> > J

>
> What is the easiest way? To use File::Find module and then use grep,
> join commands?


The easiest way would be call your Perl program with all of the file
names on the command-line:

program.pl /test/*

Then use the construct

while(<>) {
...
}

to read all of the lines in all of the files. Open two files before you
enter the while loop:

open(my $yes, '>', 'concatenate.txt') or die(...);
open(my $no, '>', 'non_concatenate.txt') or die(...);

Then in the body of the while loop, use the match operator with a
regular expression (case insensitive):

if( /keyword/i ) {
print $yes;
}else{
print $no;
}

If your shell complains about the length of the command-line, you can
use opendir and readdir to fetch the file names in your directory, then
open each file for reading individually.

--
Jim Gibson
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-25-2009
On Sat, 25 Jul 2009 09:10:08 -0700 (PDT), Rider <(E-Mail Removed)> wrote:

>Hi experts,
>
>I have a unix dir, test and it has 100 files. I need to extract all
>the lines that contain a word "keyword" key word" (case insensitive)
>from each of those files and put all those lines into another file,
>concatenate.txt. Similarly, I want all the lines that don't contain
>that key word (keyword or key word) into another file,
>non_concatenate.txt.
>
>I know a little bit of perl and I am wondering how can I do it here.
>
>Thanks,
>J


I am curious as to what end this de-interlace achieves?
There seems to be no pragmatic function to it.
Its fine if its just an exercise. Usually though, a real world
effort will reap more rewards to you personally.

Good luck!
-sln

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-26-2009
Rider <(E-Mail Removed)> wrote:
>I have a unix dir, test and it has 100 files.


opendir() and readdir() or glob().

>I need to extract all
>the lines that contain a word "keyword" key word" (case insensitive)
>from each of those files


loop through all those files who's names you gathered in step 1,
open() each, read it line by line and depening upon if the line matches
(m//), then

>and put all those lines into another file,
>concatenate.txt.


print() that line to the target file, which you open()ed earlier

> Similarly, I want all the lines that don't contain
>that key word (keyword or key word) into another file,
>non_concatenate.txt.


or the other target file, which you also open()ed earlier.

jue
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      07-26-2009
On Sat, 25 Jul 2009 09:10:08 -0700, Rider wrote:

> Hi experts,
>
> I have a unix dir, test and it has 100 files. I need to extract all the
> lines that contain a word "keyword" key word" (case insensitive) from
> each of those files and put all those lines into another file,
> concatenate.txt. Similarly, I want all the lines that don't contain that
> key word (keyword or key word) into another file, non_concatenate.txt.
>
> I know a little bit of perl and I am wondering how can I do it here.


I would not do this in perl, use the right tool for the job!

#!/bin/sh
grep -i "keyword" /path/to/test/* >/path/concatenate.txt
grep -iv "keyword" /path/to/test/* >/path/non_concatenate.txt

HTH,
M4
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      07-26-2009
>>>>> "R" == Rider <(E-Mail Removed)> writes:

R> Hi experts, I have a unix dir, test and it has 100 files. I need
R> to extract all the lines that contain a word "keyword" key word"
R> (case insensitive) from each of those files and put all those
R> lines into another file, concatenate.txt. Similarly, I want all
R> the lines that don't contain that key word (keyword or key word)
R> into another file, non_concatenate.txt.

R> I know a little bit of perl and I am wondering how can I do it
R> here.

The simplest thing would be to forgo Perl altogether and use the Unix
grep utility.

Charlton



--
Charlton Wilbur
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenating binary files Igor R. C++ 2 01-15-2009 03:54 PM
Efficiently concatenating contents of multiple files sasuke Java 5 07-06-2008 02:27 AM
[XP] Batch-print bunch of RTF files? nospam@nospam.com Python 2 06-04-2008 05:36 PM
Concatenating Audio Files in with Perl on a Mac Gary Morrison Perl 5 03-28-2006 12:45 AM
Concatenating an array into one string? Rapier Perl Misc 11 10-14-2004 10:42 AM



Advertisments