A file split problem - Unix

This is a discussion on A file split problem - Unix ; I need to split a file based on the value on those columns. So if the input file, Abc.txt, is: gut123ggg hiy545fhi fhr322hgr hfi123ryh hgp322kou jkp545kty I need to create 3 files, named Abc_123_New.txt, Abc_545_New.txt and Abc_322_New.txt. Abc_123_New.txt will have: ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: A file split problem

  1. A file split problem

    I need to split a file based on the value on those columns. So if the
    input
    file, Abc.txt, is:

    gut123ggg
    hiy545fhi
    fhr322hgr
    hfi123ryh
    hgp322kou
    jkp545kty

    I need to create 3 files, named Abc_123_New.txt, Abc_545_New.txt and
    Abc_322_New.txt.

    Abc_123_New.txt will have:
    gut123ggg
    hfi123ryh

    Abc_545_New.txt will have:
    hiy545fhi
    jkp545kty

    Abc_322_New.txt will have:
    fhr322hgr
    hgp322kou


    Even if the names of the files are Abc_1_New.txt, Abc_2_New.txt and
    Abc_3_New.txt, it will do.
    I would very much appreciate Korn Shell code for this as I am trying
    to improve my Korn Shell
    script skills. Do I need to post a new question for this?

    Thanks.

    I am doing it using Korn Shell by creating a file of all chars 4
    through 6s. Then I split the file based on this file. But this
    technique seems pretty slow and inefficient to me.


  2. Re: A file split problem

    mfarid1@yahoo.com wrote:
    > I need to split a file based on the value on those columns. So if the
    > input
    > file, Abc.txt, is:
    >
    > gut123ggg
    > hiy545fhi
    > fhr322hgr
    > hfi123ryh
    > hgp322kou
    > jkp545kty
    >
    > I need to create 3 files, named Abc_123_New.txt, Abc_545_New.txt and
    > Abc_322_New.txt.
    >
    > Abc_123_New.txt will have:
    > gut123ggg
    > hfi123ryh
    >
    > Abc_545_New.txt will have:
    > hiy545fhi
    > jkp545kty
    >
    > Abc_322_New.txt will have:
    > fhr322hgr
    > hgp322kou
    >
    >
    > Even if the names of the files are Abc_1_New.txt, Abc_2_New.txt and
    > Abc_3_New.txt, it will do.
    > I would very much appreciate Korn Shell code for this as I am trying
    > to improve my Korn Shell
    > script skills. Do I need to post a new question for this?
    >
    > Thanks.
    >
    > I am doing it using Korn Shell by creating a file of all chars 4
    > through 6s. Then I split the file based on this file. But this
    > technique seems pretty slow and inefficient to me.
    >


    awk '{num=$0; gsub(/[^[:digit:]]/,"",num); print > "Abc_"num"_New.txt"}'
    file

    Ed.

  3. Re: A file split problem

    On Feb 13, 5:20 pm, Bill Marcum wrote:
    > On 13 Feb 2007 14:57:49 -0800, mfar...@yahoo.com
    >
    >
    >
    > wrote:
    >
    > > I need to split a file based on the value on those columns. So if the
    > > input
    > > file, Abc.txt, is:

    >
    > > gut123ggg
    > > hiy545fhi
    > > fhr322hgr
    > > hfi123ryh
    > > hgp322kou
    > > jkp545kty

    >
    > > I need to create 3 files, named Abc_123_New.txt, Abc_545_New.txt and
    > > Abc_322_New.txt.

    >
    > > Abc_123_New.txt will have:
    > > gut123ggg
    > > hfi123ryh

    >
    > > Abc_545_New.txt will have:
    > > hiy545fhi
    > > jkp545kty

    >
    > > Abc_322_New.txt will have:
    > > fhr322hgr
    > > hgp322kou

    >
    > awk '{outfile="Abc_" substr($0,4,3) "_New.txt"
    > print >> outfile
    > close(outfile)}'
    >
    > --
    > I have a terrible headache, I was putting on toilet water and the lid fell.- Hide quoted text -
    >
    > - Show quoted text -


    Ed, your solution almost works. However, the file names have to be
    from the 4th through 6th characters only. They may or may not be
    digits. They also might have spaces.

    Sample data:
    gut 123ggg
    hiy 545fhi
    fhr 322hgr
    hfi 123ryh
    hgp 322kou
    jkp 545kty

    A filename would be Abc_123_New.txt.


+ Reply to Thread