Help with script

Discussion in 'Perl Misc' started by Andy, Feb 28, 2008.

  1. Andy

    Andy Guest

    Morning Guys

    I am pretty new here, tyring to learn as I go.

    I have a lil project I am trying to setup

    Basically : below is a piece of the file

    Has three fields Seperated by ~

    6081703039~1~My Own Company
    3081709039~1~DeweyCheetham&Howe
    9081710039~1~One Bad Firm
    2081757039~1~IRSUSUCK
    1082445039~1~


    Basically I need the file to only show the Field that has all data

    I am new so not sure if my syntax is correct

    If field 3 is blank then don't show?

    Can anyone help me write a Perl script to acocmplish this ?

    Thanks a bunch
     
    Andy, Feb 28, 2008
    #1
    1. Advertising

  2. Andy

    Paul Lalli Guest

    On Feb 28, 12:35 pm, Andy <> wrote:
    > Morning Guys
    >
    > I am pretty new here, tyring to learn as I go.
    >
    > I have a lil project I am trying to setup
    >
    > Basically : below is a piece of the file
    >
    > Has three fields Seperated by ~
    >
    > 6081703039~1~My Own Company
    > 3081709039~1~DeweyCheetham&Howe
    > 9081710039~1~One Bad Firm
    > 2081757039~1~IRSUSUCK
    > 1082445039~1~
    >
    > Basically I need the file to only show the Field that has all data
    >
    > I am new so not sure if my syntax is correct


    You haven't shown any syntax. How can we help you fix your syntax (if
    indeed it is broken) if you don't show it?

    > If field 3 is blank then don't show?
    >
    > Can anyone help me write a Perl script to acocmplish this ?


    Sure. We can indeed help. Best place to start would be by showing
    what you have so far, so that we can guide you towards improving and
    repairing it.

    In the meantime, here's a brief pseudo-code of what I gather you want
    to do:
    read a file line by line
    for each line,
    obtain the fields separated by hyphens
    if the number of fields equals 3
    print the line to a new file
    endif
    endfor
    replace the old file with the new one

    Paul Lalli
     
    Paul Lalli, Feb 28, 2008
    #2
    1. Advertising

  3. Andy

    Andy Guest

    On Feb 28, 12:43 pm, Paul Lalli <> wrote:
    > On Feb 28, 12:35 pm, Andy <> wrote:
    >
    >
    >
    > > Morning Guys

    >
    > > I am pretty new here, tyring to learn as I go.

    >
    > > I have a lil project I am trying to setup

    >
    > > Basically : below is a piece of the file

    >
    > > Has three fields Seperated by ~

    >
    > > 6081703039~1~My Own Company
    > > 3081709039~1~DeweyCheetham&Howe
    > > 9081710039~1~One Bad Firm
    > > 2081757039~1~IRSUSUCK
    > > 1082445039~1~

    >
    > > Basically I need the file to only show the Field that has all data

    >
    > > I am new so not sure if my syntax is correct

    >
    > You haven't shown any syntax. How can we help you fix your syntax (if
    > indeed it is broken) if you don't show it?
    >
    > > If field 3 is blank then don't show?

    >
    > > Can anyone help me write a Perl script to acocmplish this ?

    >
    > Sure. We can indeed help. Best place to start would be by showing
    > what you have so far, so that we can guide you towards improving and
    > repairing it.
    >
    > In the meantime, here's a brief pseudo-code of what I gather you want
    > to do:
    > read a file line by line
    > for each line,
    > obtain the fields separated by hyphens
    > if the number of fields equals 3
    > print the line to a new file
    > endif
    > endfor
    > replace the old file with the new one
    >
    > Paul Lalli


    Good Day

    Thanks Paul

    Ok, I have absolutely zero So far.

    Two days ago I opened the Learning Perl book and Have been trying to
    learn it

    Its not as hard as I thought, but I am trying to learn it as I work.

    lol

    Honestly the whole thing is we get a file...As the above snippet I
    pasted.

    And The script is only supposed to show the field that has a full line
    Hence when we run the script I would only see
    FIELD1 2 Field3
    6081703039~1~My Own Company
    > > 3081709039~1~DeweyCheetham&Howe
    > > 9081710039~1~One Bad Firm
    > > 2081757039~1~IRSUSUCK


    Again I apologize if my whole message was written wrong.
    I am still learning this, mind you I only picked up the book on Monday

    Thank you for your patience.
     
    Andy, Feb 28, 2008
    #3
  4. Andy wrote:
    >
    > I am pretty new here, tyring to learn as I go.
    >
    > I have a lil project I am trying to setup
    >
    > Basically : below is a piece of the file
    >
    > Has three fields Seperated by ~
    >
    > 6081703039~1~My Own Company
    > 3081709039~1~DeweyCheetham&Howe
    > 9081710039~1~One Bad Firm
    > 2081757039~1~IRSUSUCK
    > 1082445039~1~
    >
    >
    > Basically I need the file to only show the Field that has all data
    >
    > I am new so not sure if my syntax is correct
    >
    > If field 3 is blank then don't show?
    >
    > Can anyone help me write a Perl script to acocmplish this ?


    while ( <FH> ) {
    chomp;
    my @fields = split /~/, $_, -1;

    if ( 3 != @fields ) {
    warn "Error: should be 3 fields, record contains " . @fields .
    " fields instead.\n";
    next;
    }

    if ( 3 != grep length, @fields ) {
    warn "Error: one of the fields is empty.\n";
    next;
    }
    # Or, just the third field:
    unless ( length $fields[ 2 ] ) {
    warn "Error: the third field is empty.\n";
    next;
    }

    # process the data
    }



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Feb 28, 2008
    #4
  5. Andy

    Andy Guest

    On Feb 28, 1:15 pm, "John W. Krahn" <> wrote:
    > Andy wrote:
    >
    > > I am pretty new here, tyring to learn as I go.

    >
    > > I have a lil project I am trying to setup

    >
    > > Basically : below is a piece of the file

    >
    > > Has three fields Seperated by ~

    >
    > > 6081703039~1~My Own Company
    > > 3081709039~1~DeweyCheetham&Howe
    > > 9081710039~1~One Bad Firm
    > > 2081757039~1~IRSUSUCK
    > > 1082445039~1~

    >
    > > Basically I need the file to only show the Field that has all data

    >
    > > I am new so not sure if my syntax is correct

    >
    > > If field 3 is blank then don't show?

    >
    > > Can anyone help me write a Perl script to acocmplish this ?

    >
    > while ( <FH> ) {
    > chomp;
    > my @fields = split /~/, $_, -1;
    >
    > if ( 3 != @fields ) {
    > warn "Error: should be 3 fields, record contains " . @fields .
    > " fields instead.\n";
    > next;
    > }
    >
    > if ( 3 != grep length, @fields ) {
    > warn "Error: one of the fields is empty.\n";
    > next;
    > }
    > # Or, just the third field:
    > unless ( length $fields[ 2 ] ) {
    > warn "Error: the third field is empty.\n";
    > next;
    > }
    >
    > # process the data
    > }
    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you
    > can special-order certain sorts of tools at low cost and
    > in short order. -- Larry Wall


    John

    Thank you Very Much

    I take it I have to turn around and

    Add

    !#/usr/bin/perl -w


    Question How does the script know that I want it to Scrub a particular
    file.

    In this case the Above snippet is from a file called

    msus.dat

    ?
     
    Andy, Feb 28, 2008
    #5
  6. Andy wrote:
    > On Feb 28, 1:15 pm, "John W. Krahn" <> wrote:
    >> Andy wrote:
    >>
    >>> I am pretty new here, tyring to learn as I go.
    >>> I have a lil project I am trying to setup
    >>> Basically : below is a piece of the file
    >>> Has three fields Seperated by ~
    >>> 6081703039~1~My Own Company
    >>> 3081709039~1~DeweyCheetham&Howe
    >>> 9081710039~1~One Bad Firm
    >>> 2081757039~1~IRSUSUCK
    >>> 1082445039~1~
    >>> Basically I need the file to only show the Field that has all data
    >>> I am new so not sure if my syntax is correct
    >>> If field 3 is blank then don't show?
    >>> Can anyone help me write a Perl script to acocmplish this ?

    >> while ( <FH> ) {
    >> chomp;
    >> my @fields = split /~/, $_, -1;
    >>
    >> if ( 3 != @fields ) {
    >> warn "Error: should be 3 fields, record contains " . @fields .
    >> " fields instead.\n";
    >> next;
    >> }
    >>
    >> if ( 3 != grep length, @fields ) {
    >> warn "Error: one of the fields is empty.\n";
    >> next;
    >> }
    >> # Or, just the third field:
    >> unless ( length $fields[ 2 ] ) {
    >> warn "Error: the third field is empty.\n";
    >> next;
    >> }
    >>
    >> # process the data
    >> }

    >
    > John
    >
    > Thank you Very Much
    >
    > I take it I have to turn around and
    >
    > Add
    >
    > !#/usr/bin/perl -w


    Better to add:

    #!/usr/bin/perl
    use warnings;
    use strict;

    > Question How does the script know that I want it to Scrub a particular
    > file.


    It doesn't. The example posted is incomplete.

    > In this case the Above snippet is from a file called
    >
    > msus.dat


    Open the file first:

    open FH, '<', 'msus.dat' or die "Cannot open 'msus.dat' $!";



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Feb 28, 2008
    #6
  7. Andy

    John Bokma Guest

    Andy <> wrote:

    > Basically : below is a piece of the file
    >
    > Has three fields Seperated by ~
    >
    > 6081703039~1~My Own Company
    > 3081709039~1~DeweyCheetham&Howe
    > 9081710039~1~One Bad Firm
    > 2081757039~1~IRSUSUCK
    > 1082445039~1~
    >
    >
    > Basically I need the file to only show the Field that has all data
    >
    > I am new so not sure if my syntax is correct


    what syntax?

    > If field 3 is blank then don't show?


    perl -ne "/~$/ or print;" yf.txt

    or

    perl -ne "print unless /~$/" yf.txt

    (yf.txt is your file :) )

    (assuming Windows, otherwise you might want to use single quotes)

    Note: this assumes that the 3rd field can never end in ~
    and that the 3rd field blank means empty.

    --
    John

    http://johnbokma.com/mexit/2008/02/
     
    John Bokma, Feb 28, 2008
    #7
  8. Andy

    Andy Guest

    On Feb 28, 3:04 pm, John Bokma <> wrote:
    > Andy <> wrote:
    > > Basically : below is a piece of the file

    >
    > > Has three fields Seperated by ~

    >
    > > 6081703039~1~My Own Company
    > > 3081709039~1~DeweyCheetham&Howe
    > > 9081710039~1~One Bad Firm
    > > 2081757039~1~IRSUSUCK
    > > 1082445039~1~

    >
    > > Basically I need the file to only show the Field that has all data

    >
    > > I am new so not sure if my syntax is correct

    >
    > what syntax?
    >
    > > If field 3 is blank then don't show?

    >
    > perl -ne "/~$/ or print;" yf.txt
    >
    > or
    >
    > perl -ne "print unless /~$/" yf.txt
    >
    > (yf.txt is your file :) )
    >
    > (assuming Windows, otherwise you might want to use single quotes)
    >
    > Note: this assumes that the 3rd field can never end in ~
    > and that the 3rd field blank means empty.
    >
    > --
    > John
    >
    > http://johnbokma.com/mexit/2008/02/


    John

    thank you that worked wonderfully

    although I am trying to understand why.

    But as I was playing around I removed some characters from the file

    and it prints data from lines that I removed the ~1~ For instance the
    1

    Can u explain some of this ,

    Pardon if I am asking to much, but I am curious.

    I have been wanting to delve into perl for a long while.
     
    Andy, Feb 28, 2008
    #8
  9. Andy <> wrote:
    > On Feb 28, 12:43 pm, Paul Lalli <> wrote:
    >> On Feb 28, 12:35 pm, Andy <> wrote:



    >> > I am new so not sure if my syntax is correct

    >>
    >> You haven't shown any syntax. How can we help you fix your syntax (if
    >> indeed it is broken) if you don't show it?



    > Ok, I have absolutely zero So far.



    Then what syntax was it that were referring to?

    To me, it sounds like you want someone to write your program for
    you rather then someone to help you learn enough to write your
    program yourself...


    > Two days ago I opened the Learning Perl book and Have been trying to
    > learn it



    That's a good start.

    The standard docs that ship with Perl will be very useful to you too.


    > Honestly the whole thing is we get a file



    Then to progress beyond the zero that you have so far,
    write a program than can read the file.

    (Post it here if your code does not do what you intended.)

    Then modify it to output all of the lines.

    Then modify it to recognize and output only the lines you want.


    > Thank you for your patience.



    Write some Perl code.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 29, 2008
    #9
  10. Andy <> wrote:

    > Subject: Help with script



    Please put the subject of your article in the Subject of your article.


    > Morning Guys



    This is an international forum, so it is simultaneously night and day...


    > I am pretty new here,



    Please see the Posting Guidelines that are posted here frequently.


    > tyring to learn as I go.



    The best way to learn Perl is to write Perl code.


    > Has three fields Seperated by ~
    >
    > 6081703039~1~My Own Company
    > 3081709039~1~DeweyCheetham&Howe
    > 9081710039~1~One Bad Firm
    > 2081757039~1~IRSUSUCK
    > 1082445039~1~
    >
    >
    > Basically I need the file to only show the Field that has all data



    perl -ne 'print if /.~.~./' tilde_file


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 29, 2008
    #10
    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. dpackwood
    Replies:
    3
    Views:
    1,808
  2. Rajat
    Replies:
    3
    Views:
    703
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    565
    Jan Kaliszewski
    Jan 7, 2010
  4. Greg
    Replies:
    1
    Views:
    169
    Gunnar Hjalmarsson
    Jun 6, 2005
  5. Replies:
    9
    Views:
    174
Loading...

Share This Page