Re: Put ID at beginning of data lines

"RAKESH" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
This is one way :


:loop
   $!N
   /\n"/{P;D;}
   s/\n/,/
tloop



--- In [email protected], "Eliana" <elianaspda@...> wrote:
>
> I never quite got a good handle on the use of hold space, so I need help with taking a file which has identifying information for a person on one line, followed by data points on lines starting with a blank space for each data point.  There could be anywhere from 1 to dozens of data  lines per individual in the file.
> 
> I am running GNU sed version 3.02 on Linux.
> 
> Here is a sample input file:
> 
> "1","V3","Johnny Jamison"
>  144058604,149864828
>  104132262,115218852
>  40739561,80546082
> "2","V2","Janet Anne Doe"
>  145956371,150258170
>  125505743,131005138
>  53487753,77418759
>  32142804,34367848
> "3","V2","William Jones"
>  121742874,124782881
>  89861384,107281037
> 
> Note that some people may have identical ID information in the first field, so ^" is what I am using to indicate that the line is the one with ID informtion on it.
>  
> The output I would like to generate from the above input data would look like the following:
> 
> "1","V3","Johnny Jamison",144058604,149864828
> "1","V3","Johnny Jamison",104132262,115218852
> "1","V3","Johnny Jamison",40739561,80546082
> "2","V2","Janet Anne Doe",145956371,150258170
> "2","V2","Janet Anne Doe",125505743,131005138
> "2","V2","Janet Anne Doe",53487753,77418759
> "2","V2","Janet Anne Doe",32142804,34367848
> "3","V2","William Jones",121742874,124782881
> "3","V2","William Jones",89861384,107281037  
> 
> It took quite a bit of work to take the original single line format for the data for each person, and turn it into the input format I am showing you above.  But now that I have massaged the data into something I think should easier to work with, I am stumped.
> 
> If I try the standard code for joining lines, I get the data all merged back together again:
> 
> $ sed  -e :a -e '$!N;s/\n /,/;ta' -e  'P;D'  testx.txt
> "1","V3","Johnny Jamison",144058604,149864828,104132262,115218852,40739561,80546082
> "2","V2","Janet Anne Doe",145956371,150258170,125505743,131005138,53487753,77418759,32142804,34367848
> "3","V2","William Jones",121742874,124782881,89861384,107281037 
> $
> 
> Putting code into a sed script, I tried:
> 
> $ cat x.sed 
> /^"/,/^ /{
> h
> G 
> s/^\(".*"\)\n /\1,/
> } 
> 
> And I got duplicate lines and no prepending of ID information:
> 
> $ sed  -f x.sed  testx.txt
> 1","V3","Johnny Jamison"
> "1","V3","Johnny Jamison"
>  144058604,149864828
>  144058604,149864828
>  104132262,115218852
>  40739561,80546082
> "2","V2","Janet Anne Doe"
> "2","V2","Janet Anne Doe"
>  145956371,150258170
>  145956371,150258170
>  125505743,131005138
>  53487753,77418759
>  32142804,34367848
> "3","V2","William Jones"
> "3","V2","William Jones"
>  121742874,124782881
>  121742874,124782881
>  89861384,107281037
> $ 
> 
> I have tried a number of other ways, with varied (and often much uglier) results, none getting any closer to the desired  output.
> 
> I think I do prefer using a sed script for this than trying to make a one liner.
> 
> Suggestions?
> 
> Thanks,
> Eliana
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.