Re: Put ID at beginning of data lines

"Eliana" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <TkcMail.1337047863.8918.0@zaurus>
Thank you.  Your output looks good, but I am not sure my sed can take the -n option, and am not at all sure why this crashed.  
$ cat testx.txt|sed  -ne '/^"/h;/^[^"]/{G;s/\([0-9,]\+\)\n\(.*\)/\2,\1/p}' 
sed: -e expression #1, char 47: Unknown option
to 's'
$ 
 
When I transfer to a sed script, it complains about this line having an unknown option, I think it is the + that  is not recognized:

 s/\([0123456789,]\+\)\n\(.*\)/\2,\1/p 

I had to expand the numbers as my sed does not expand 0-9.

Next step?  What can take the place of that plus sign?

I also am not sure my sed will treat \n properly, but have not gotten that far yet.

eliana

> I tried this:
> $ sed -ne '/^"/h;/^[^"]/{G;s/\([0-9,]\+\)\n\(.*\)/\2,\1/p}'
> data.1
> and got this:
> "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
> explained:
> sed -ne '
> /^"/h;
> # if the line starts with
> quotes (the id string) hold that line
> /^[^"]/{
> # this block is for lines
> that don't start with quotes (our
> data lines) G;
> # first get
> that part that was held and
> concatenate it with the current line
> s/\([0-9,]\+\)\n\(.*\)/\2,\1/p
> # put the stuff that is data in
> the end of the line and leave the
> rest in the beggining, and also
> remove the newline so it all stays in
> one line }'
> # end of block
> 2012/5/14 Eliana
> <[email protected]> 
> > 
> > 
> > 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
> > 
> > 
> > ...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
> >
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.