split file

Discussion in 'Perl Misc' started by Babu, Dec 29, 2007.

  1. Babu

    Babu Guest

    Hi,

    I am new to perl.I have basic question about splitting a file into 2.

    the input file= temp.hex

    contents

    4A
    5F
    C3
    3D

    is there a one liner to split this file into 2 files temp1.hex
    containing

    4A
    5F


    and temp2.hex having

    C3
    3D


    Thanks in advance

    SN
    Babu, Dec 29, 2007
    #1
    1. Advertising

  2. On Babu <> wrote:
    >I am new to perl.I have basic question about splitting a file into 2.
    >
    >the input file= temp.hex
    >
    >contents
    >
    >4A
    >5F
    >C3
    >3D
    >
    >is there a one liner to split this file into 2 files temp1.hex
    >containing
    >
    >4A
    >5F
    >
    >
    >and temp2.hex having
    >
    >C3
    >3D


    Probably there is. But unless you tell us the criteria for _where_ to split
    it's everyone's guess as what the algorithm is supposed to do.
    - split after a given value 5F?
    - split before a given value C3?
    - split after 2 lines?
    - split between given values 5F and C3?
    - split one line after a given value 4A?
    - ....

    In any case, did you check the standard program split? Maybe it already does
    what you are looking for.

    jue
    Jürgen Exner, Dec 29, 2007
    #2
    1. Advertising

  3. Babu wrote:
    >
    > I am new to perl.I have basic question about splitting a file into 2.
    >
    > the input file= temp.hex
    >
    > contents
    >
    > 4A
    > 5F
    > C3
    > 3D
    >
    > is there a one liner to split this file into 2 files temp1.hex
    > containing
    >
    > 4A
    > 5F
    >
    >
    > and temp2.hex having
    >
    > C3
    > 3D


    perl -e'$h = ( @x = <> ) / 2 + .5; open _ and print _ splice @x, 0, $h
    for map ">$_", qw/temp1.hex temp2.hex/'



    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, Dec 29, 2007
    #3
    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. Replies:
    2
    Views:
    468
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    697
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    216
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    240
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    609
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page