Professional Web Applications Themes

Need help with script - PERL Beginners

I have a file with the following format Object1 "Description1" Object2 "Description" Object3 "Description" I would like the output in the following format object1<...tab....>Description1 object2<...tab....>Description2 object3<...tab....>Description3 Thanks...

  1. #1

    Default Need help with script

    I have a file with the following format

    Object1
    "Description1"

    Object2
    "Description"

    Object3
    "Description"

    I would like the output in the following format
    object1<...tab....>Description1
    object2<...tab....>Description2
    object3<...tab....>Description3

    Thanks

    PerlDiscuss Guest

  2. #2

    Default Re: Need help with script

    Please use a more descriptive subject line....
     

    What have you tried, what have you researched, where did you fail? This
    is not a free script writing service, and your question looks like homework.

    http://www.catb.org/~esr/faqs/smart-questions.html

    http://danconia.org
    Wiggins Guest

  3. #3

    Default Re: Need help with script

    On Wed, 2004-09-29 at 18:55, PerlDiscuss - Perl Newsgroups and mailing
    lists wrote: 

    perl -lne 'BEGIN{$/="\n\n";}s/\n/\t/;print' FILENAME

    HTH
    Ram


    Ramprasad Guest

  4. #4

    Default Re: Need help with script

    Ramprasad A Padmanabhan wrote: 
    >
    > perl -lne 'BEGIN{$/="\n\n";}s/\n/\t/;print' FILENAME[/ref]


    perl -l -00pe's/\n/\t/' FILENAME



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  5. #5

    Default Re: Need help with script

    >>> I would like the output in the following format 
    >>
    >>
    >> perl -lne 'BEGIN{$/="\n\n";}s/\n/\t/;print' FILENAME[/ref]
    >
    >
    >
    > perl -l -00pe's/\n/\t/' FILENAME[/ref]

    That's pretty slick you guys, he's sure to get an A+ ;)

    If your teacher requires the quotes to be removed:

    perl -l -00pe's/\n/\t/;s/\"//g;' FILENAME

    :)
    JupiterHost.Net Guest

  6. #6

    Default Re: Need help with script

    Hi Perlers,

    On 30 Sep 2004 10:11:29 +0100, Jose Alves de Castro
    <di.uminho.pt> wrote: 
    > >
    > > That's pretty slick you guys, he's sure to get an A+ ;)
    > >
    > > If your teacher requires the quotes to be removed:[/ref]
    >
    > What if the teacher requires an explanation? O:-)
    >
    > It is my opinion that code should be explained, at least in this list.
    > You're trying to teach people how to fish (and maybe swim). Giving them
    > fish is good, of course, but tell them how you got it :-)
    >
    > That said, nice code :-)
    >  
    > --
    > Josť Alves de Castro <org>
    > http://natura.di.uminho.pt/~jac
    > [/ref]

    I'll give it a try.

    First, it's good to know that the two Perl special variables '$/' and
    '$\' are the input separator and output separator. By Default, they
    will be $/ = \n (newline character) and $\ = undef (nothing. No
    output separator).

    Now, on the command line, the '-0' option will set the input separator
    ($/). In the above example, it's setting $/ = 0. Also, in the
    example, the '-l' will do two things. First, it will automatically
    chomp() whatever's in '$/', and then it will set the output separator
    to be whatever the input separator will be. So, specific to our
    example, first '-l' sets '$\' (output separator) to whatever '$/' is
    (at this point, it's \n, or a newline). Then, the '-0' switch is
    setting the $/ = 0 ( or null, or nothing!).

    OK, next we have '-p' and '-e'. The '-e' tells Perl to read one line
    (the one after the '-e') and use that as the code to process. The
    '-p' causes Perl to assume the following code around your code:

    LINE:
    while( <> ) {
    # your code goes here
    } continue {
    print or die "-p destination: $!\n";
    }

    So, this is going to process whatever files it finds on your command
    line and then print '$_'!

    Now, the code that's going into that block is, in our example:
    s/\n/\t/;
    s/\"//g;

    So, we get this as the code Perl is running:

    LINE:
    while( <> ) {
    s/\n/\t/; # Change newlines into tabs
    s/\"//g; # Remove all double-quotes
    } continue {
    print or die "-p destination: $!\n";
    }

    but with the special $/ = 0 as the input separator and $\ = \n as the
    output separator!

    There! Am I right? This is fun ... we should do this more often!
    This taught me a lot.

    BTW, I found most of these explanations in the 'perldoc perlrun' and
    'perldoc perlvar' pages. You can check out continue blocks with
    'perldoc -f continue'.

    --Errin
    Errin Guest

  7. #7

    Default Re: Need help with script

    On Thu, Sep 30, 2004 at 08:00:41AM -0500, Errin Ln wrote:
     
     [/ref][/ref]
     [/ref]

    And it normally is. But if someone posts a message saying "please do
    this for me" without (apparently) making any effort to do it themselves,
    then a functioning cryptic one-liner response is a succinct way of
    saying that as soon as you put a little more effort into this then so
    will we.
     

    Pretty close:

    $ perl -MO=Dep -l00pe's/\n/\t/;s/\"//g'
    BEGIN { $/ = "\n"; $\ = "\000"; }
    LINE: while (defined($_ = <ARGV>)) {
    chomp $_;
    s/\n/\t/;
    s/"//g;
    }
    continue {
    print $_;
    }
    -e syntax OK

    which shows a little confusion over $/ and $\, and an unnecessary \ in the
    initial program.
     

    Good :-)

    --
    Paul Johnson - net
    http://www.pjcj.net
    Paul Guest

  8. #8

    Default Re: Need help with script

    Paul Johnson wrote:
     
     [/ref]
     [/ref][/ref]
     [/ref][/ref]
     
     [/ref]
     
     
     
     [/ref]
     

    Thanks for your help guys...

    But the code is performing the logic only for the first set of lines...

    After the running the above script, the output looks like

    Object1<...tab...>Description1

    Object2
    Description2

    Object3
    Description3





    PerlDiscuss Guest

  9. #9

    Default RE: Need help with script

    From: Errin Ln <mailto:com> wrote:

    [snip]
    : There! Am I right? This is fun ... we should do this
    : more often! This taught me a lot.

    That's one advantage of answering questions on a list
    like this. You learn while researching the answer. Most of
    my experience with perl comes from researching answers to
    beginner questions on these lists.


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328


    Charles Guest

  10. #10

    Default Re: Need help with script

    Hi Paul,
    Thx for the response

    On Thu, 30 Sep 2004 15:30:06 +0200, Paul Johnson <net> wrote:

    <<SNIP>>
     
    >
    > Good :-)
    >
    > --
    > Paul Johnson - net[/ref]


    When I run your command line up there, I get the following:

    # perl -MO=Dep -l00pe's/\n/\t/;s/\"//g'
    LINE: while (defined($_ = <ARGV>)) {
    chomp $_;
    s/\n/\t/;
    s/"//g;
    }
    continue {
    print $_;
    }
    -e syntax OK

    What OS are you running? My '-MO=Dep' didn't create that BEGIN
    Block. I'm on Solaris, using Perl 5.6.1. I'm just curious what the
    difference is.

    --Errin

    BTW, I didn't know about the Dep Pre-Compiler thing! Thanks for
    pointing it out. It's very handy. Why do you think Perl uses:

    while( defined( $_ = <ARGV> ) )

    instead of:

    while( <> )

    Is this example pointing out that the diamond ( <> ) operator is
    really a short-cut for 'defined( $_ = <ARGV> )' ? I'll have to go
    read about this. What is Perl protecting against by putting that
    assignment in a defined()?
    Errin Guest

  11. #11

    Default Re: Need help with script

    On Thu, Sep 30, 2004 at 11:26:27AM -0500, Errin Ln wrote:
     

    That was on linux, but the important thing here is the perl version.
    5.6.1 is old, and if you are looking at the B modules (which Dep is)
    it's very old. There have been many improvements (bug fixes) since
    then.
     

    Yes, exactly that.
     

    It's protecting against input which evaluates to false, such as the last
    line of a file which contains only 0 and no newline, for example.

    --
    Paul Johnson - net
    http://www.pjcj.net
    Paul Guest

  12. #12

    Default Re: Need help with script

    > Thanks for your help guys... 

    Can you post EXACTLY what's in the input file for us?

    I test with the following input file, I called it object.txt:
    # cat object.txt
    Object1
    "Description1"

    Object2
    "Description2"

    Object3
    "Description3"

    I run this command line:
    # perl -l -00pe's/\n/\t/;s/\"//g' object.txt
    Object1 Description1
    Object2 Description2
    Object3 Description3

    It's hard to see the tabs, so I tried one with 2 tabs in it for clarity:
    # perl -l -00pe's/\n/\t\t/;s/\"//g' object.txt
    Object1 Description1
    Object2 Description2
    Object3 Description3

    So, on my (Solaris 9, Perl 5.6.1) box, it's working. What OS and Perl
    version are you using and what's your input file look like?

    --Errin
    Errin Guest

  13. #13

    Default Re: Need help with script

    I am using Cygwin on Win2K and the version of perl on it is
    v5.8.0

    I am using the same input file, but when I run the command you ran, the
    output looks like

    Object1 Description1

    Object2
    Description2

    Object3
    Description3

    Thanks


    Errin Ln wrote:
     [/ref]
     
     
     
     
     
     
     
     


    PerlDiscuss Guest

  14. #14

    Default Re: Need help with script

    On 30 Sep 2004 19:52:31 -0000, PerlDiscuss - Perl Newsgroups and
    mailing lists <com> wrote: 


    Hi again,

    Just to let you know, most on this list will become upset with you if
    you don't bottom-post. For future reference ...


    Ok, I've got ActiveState on WinXP, 5.8.4 ... I tried and found that I
    had the same problems as you. After much playing around, I found it's
    a quoting problem on the command line (at least, in my case it was).
    I just don't have a good grasp of quoting rules and precedence in DOS
    I guess. Here's what I did to make it work on my DOS command line:

    C:\ > perl -l -00pe s/\n/\t/;s/\"//g object.txt

    I basically just removed the single-quotes from around the code in the
    '-e' option. That gave me the output I was looking for. Try it and
    let us know.

    --Errin

    BTW, anyone have a good reference for DOS quoting rules/precedence?
    Errin Guest

  15. #15

    Default Re: Need help with script

    > Hi again,
     
     
     
     
     

    If I remove single quotes, my command kind of hangs..it does not give any
    output.

    thanks


    PerlDiscuss Guest

  16. #16

    Default Re: Need help with script

    Errin Ln wrote: 
    >>
    >>What if the teacher requires an explanation? O:-)
    >>
    >>It is my opinion that code should be explained, at least in this list.
    >> You're trying to teach people how to fish (and maybe swim). Giving them
    >> fish is good, of course, but tell them how you got it :-)
    >>
    >>That said, nice code :-)
    >> [/ref]
    >
    > I'll give it a try.
    >
    > First, it's good to know that the two Perl special variables '$/' and
    > '$\' are the input separator and output separator. By Default, they
    > will be $/ = \n (newline character) and $\ = undef (nothing. No
    > output separator).[/ref]

    So far so good. :-)
     

    Wrong.
     

    perldoc perlrun
    [snip]
    -0[octal/hexadecimal]
    specifies the input record separator ($/) as an octal or hexadecimal
    number. If there are no digits, the null character is the separator.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
    Other switches may precede or follow the digits. For example, if you
    have a version of find which can print filenames terminated by the null
    character, you can say this:

    find . -name '*.orig' -print0 | perl -n0e unlink

    The special value 00 will cause Perl to slurp files in paragraph mode.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
    The value 0777 will cause Perl to slurp files whole because there is no
    legal byte with that value.


    So using the switch -00 is the same as setting the input record separator to
    paragraph mode or $/ = '';



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  17. #17

    Default Re: Need help with script

    Paul Johnson wrote: [/ref]
    >
    > $ perl -MO=Dep -l00pe's/\n/\t/;s/\"//g'
    > BEGIN { $/ = "\n"; $\ = "\000"; }[/ref]

    In your example you have removed the -0 switch so it is doing something
    completely different.


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  18. #18

    Default Re: Need help with script

    On Thu, Sep 30, 2004 at 04:44:36PM -0700, John W. Krahn wrote:
     
    > >
    > >$ perl -MO=Dep -l00pe's/\n/\t/;s/\"//g'
    > >BEGIN { $/ = "\n"; $\ = "\000"; }[/ref]
    >
    > In your example you have removed the -0 switch so it is doing something
    > completely different.[/ref]

    Oops. Quite right.

    Must remember not to stop checking my posts until they are correct.

    Thanks, John.

    --
    Paul Johnson - net
    http://www.pjcj.net
    Paul Guest

Similar Threads

  1. ksh script - Preventing multiple runs of a particular script
    By Melissa in forum Linux / Unix Administration
    Replies: 4
    Last Post: June 26th, 01:49 PM
  2. 6.1 script not working on 7.0 server. Script used towork!
    By piaposa in forum Coldfusion Flash Integration
    Replies: 1
    Last Post: February 9th, 03:43 PM
  3. Replies: 2
    Last Post: January 24th, 06:00 AM
  4. Execute shell script from a perl script
    By kderaedt in forum Perl / CGI
    Replies: 3
    Last Post: June 26th, 08:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139