Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    52

    Unanswered: Help with Selective printing in Sed (accepting other suggestions)

    Hi all,

    I am really in need of some light here. Any suggestions deeply appreciated !
    I have a relative simple problem but there is something missing.

    This is the task: I have a file with N lines. N is composed of M subgroups. For instance, let's say that each subgroup has 3 lines :

    Line 1 : A1 (A1 is actually composed by a few columns)
    Line 2 : A2
    Line 3 : A3
    Line 4 : B1
    Line 5 : B2
    Line 6 : B3
    ...
    Line N : 3xM

    I just need to filter this file and put every n-th line in a different file :

    File 1
    Line 1 : A1
    Line 2 : B1
    Line 3 : C1
    ...

    File 2
    Line 1 : A2
    Line 2 : B2
    Line 3 : C2
    ...

    File 3
    Line 1 : A3
    Line 2 : B3
    Line 3 : C3
    ...

    Initially I tried to use "sed -n" like this :

    sed -n '1,${p;n;n;}' data > result

    This works only for small subgroups like the one above. If data has millions of lines with thousands of subgroups, it is impossible to run sed command (because it has thousands of "n;n;n...").

    Does someone know how to perform an efficient selective printing ?
    Thanks !

  2. #2
    Join Date
    Feb 2004
    Posts
    52
    Solved.
    For the record, instead of echoing thousands of "n;" in a single line, I included a \n (new line) every hundred or so. Not optimal but it works.

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    I'm not a sed guy, but I'd do this in perl:

    perl -MIO::Handle -e 'open $h[$_], ">", "file$_" for 1..10; $c=1; while(<>) { $h[$c++]->print($_); $c=1 if $c > 10; } close $h[$_] for 1..10' < data

    That will split the input over 10 separate files named file1 through file10. Change 10 to whatever number you want.

Posting Permissions

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