If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help with Selective printing in Sed (accepting other suggestions)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-02-08, 22:28
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
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 !
Reply With Quote
  #2 (permalink)  
Old 07-03-08, 11:30
Serg Serg is offline
Registered User
 
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.
Reply With Quote
  #3 (permalink)  
Old 07-05-08, 20:01
sco08y sco08y is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On