Re: Put ID at beginning of data lines
Jan FRS <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <CAL-k7jHyx3NwAShmLMd56_prxSvmdi63nciL4MJVVSfbph5p2w@mail.gmail.com> |
$ cat data.1
"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
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
Jan Silva
[Non-text portions of this message have been removed]