Concatenating selective lines from a bunch of files into a singlefile

R

Rider

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
 
R

Rider

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
 
J

Jim Gibson

Rider said:
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.
 
S

sln

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
 
J

Jürgen Exner

Rider said:
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
 
M

Martijn Lievaart

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
 
C

Charlton Wilbur

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top