creating a new file

M

MJS

I need to write a script which when run will create a new text file
depending if there is another text file which contains certain string
e.g. [abc]. The file should be created on the strings first occurance
and the rest occurances, if any, should be ignored.

Thanks in anticipation
 
T

Tad McClellan

MJS said:
I need to write a script which when run will create a new text file
depending if there is another text file which contains certain string
e.g. [abc]. The file should be created on the strings first occurance
and the rest occurances, if any, should be ignored.

Thanks in anticipation


Anticipation of what?

An answer?

To what? You did not ask a question.



What part are you stuck on?

Show us the code you have so far, and we will help you fix it.
 
N

Nick Pinckernell

#Open the 1st file,

open(IN, "file1") or die "Can't open file1: $!\n";

#search for the string
while(<IN>) {
if(/abc/) {
#found 'abc' so open the 2nd file for writing
open(OUT, ">file2") or die "Can't open file2: $!\n";
print OUT "foo\n";
close(OUT);
}
}
close(IN);

Have not tested this code,
but, hope that helps
_nick
 
T

Ted Zlatanov

#Open the 1st file,

open(IN, "file1") or die "Can't open file1: $!\n";

#search for the string
while(<IN>) {
if(/abc/) {
#found 'abc' so open the 2nd file for writing
open(OUT, ">file2") or die "Can't open file2: $!\n";
print OUT "foo\n";
close(OUT);
}
}
close(IN);

Have not tested this code,

Why not?
but, hope that helps

I don't think it does. Also, you don't exit after the first ocurrence
of the search string as the original request said.

perl -n -e 'if (m/abc/) { open (OUT, ">file2"); exit; }' file1

Ted
 
T

Ted Zlatanov

I need to write a script which when run will create a new text file
depending if there is another text file which contains certain
string e.g. [abc]. The file should be created on the strings first
occurance and the rest occurances, if any, should be ignored.

Thanks in anticipation


Anticipation of what?

An answer?

To what? You did not ask a question.



What part are you stuck on?

Show us the code you have so far, and we will help you fix it.

For the record, although I provided an answer to correct Nick's
nonfunctional one (that irked me worse than the original post), I
agree with your sentiment. It was especially galling that MJS said "I
need to write a script" but then sat back "in anticipation."

Ted
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top