creating a new file

Discussion in 'Perl Misc' started by MJS, Aug 12, 2003.

  1. MJS

    MJS Guest

    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
     
    MJS, Aug 12, 2003
    #1
    1. Advertising

  2. MJS <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 12, 2003
    #2
    1. Advertising

  3. #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
     
    Nick Pinckernell, Aug 12, 2003
    #3
  4. MJS <> wrote:

    > I need to write a script



    I need to lose 15 pounds.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 12, 2003
    #4
  5. MJS

    Ted Zlatanov Guest

    On Tue, 12 Aug 2003, wrote:


    >#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
     
    Ted Zlatanov, Aug 12, 2003
    #5
  6. MJS

    Ted Zlatanov Guest

    On Tue, 12 Aug 2003, wrote:

    MJS <> wrote:
    >
    >> 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
     
    Ted Zlatanov, Aug 12, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    498
    Guest
    Jul 29, 2003
  2. C-man
    Replies:
    9
    Views:
    661
    Roedy Green
    Apr 11, 2004
  3. J
    Replies:
    0
    Views:
    231
  4. Jelle Smet
    Replies:
    5
    Views:
    586
    Piet van Oostrum
    May 9, 2009
  5. Replies:
    2
    Views:
    501
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page