From: tntelle on
I have a ton of data, and I am trying to do something where once a
regexp is found, it appends the last occurance before that match to a
differnet regexp

Like:

Data: Something1
Other: 1
Something Else
Something Else
Data: Something2
Other: 3
Something Else
Something Else
Data: Something3
Other: 3
Something Else
Something Else
Data: Something4
Something Else
Something Else
Other: 1
Something Else
Something Else
Data: Something5
Other: 1


So if i want to find all occurances of "Other 1" Print that, and then
append the last occurance of what "Data: X"
So the above would become:

Other: 1, Data: Something1
Other: 1, Data: Something4
Other: 1, Data: Something5


I've been around the world and back trying to find the answer to this,
and I feel I am looking too hard. Any help would be greatly
appreciated.

Thank you!



From: Rajan on


<tntelle(a)yahoo.com> wrote in message
news:533eb791-ad84-4cd7-b4ff-2bdc7ca98e17(a)k37g2000hsf.googlegroups.com...
> I have a ton of data, and I am trying to do something where once a
> regexp is found, it appends the last occurance before that match to a
> differnet regexp
>
> Like:
>
> Data: Something1
> Other: 1
> Something Else
> Something Else
> Data: Something2
> Other: 3
> Something Else
> Something Else
> Data: Something3
> Other: 3
> Something Else
> Something Else
> Data: Something4
> Something Else
> Something Else
> Other: 1
> Something Else
> Something Else
> Data: Something5
> Other: 1
>
>
> So if i want to find all occurances of "Other 1" Print that, and then
> append the last occurance of what "Data: X"
> So the above would become:
>
> Other: 1, Data: Something1
> Other: 1, Data: Something4
> Other: 1, Data: Something5
>
>
> I've been around the world and back trying to find the answer to this,
> and I feel I am looking too hard. Any help would be greatly
> appreciated.
>
> Thank you!
>
>
>

I realised that something gawky things go on in this group.

This should do
gawk '/^Data/ {last=$0}
/^Other: 1$/{print $0 ", " last}'


From: Rajan on


"Rajan" <svrajan(a)rediffmail.com> wrote in message
news:480e6ad4$0$90266$14726298(a)news.sunsite.dk...
>
>
> <tntelle(a)yahoo.com> wrote in message
> news:533eb791-ad84-4cd7-b4ff-2bdc7ca98e17(a)k37g2000hsf.googlegroups.com...
>> I have a ton of data, and I am trying to do something where once a
>> regexp is found, it appends the last occurance before that match to a
>> differnet regexp
>>
>> Like:
>>
>> Data: Something1
>> Other: 1
>> Something Else
>> Something Else
>> Data: Something2
>> Other: 3
>> Something Else
>> Something Else
>> Data: Something3
>> Other: 3
>> Something Else
>> Something Else
>> Data: Something4
>> Something Else
>> Something Else
>> Other: 1
>> Something Else
>> Something Else
>> Data: Something5
>> Other: 1
>>
>>
>> So if i want to find all occurances of "Other 1" Print that, and then
>> append the last occurance of what "Data: X"
>> So the above would become:
>>
>> Other: 1, Data: Something1
>> Other: 1, Data: Something4
>> Other: 1, Data: Something5
>>
>>
>> I've been around the world and back trying to find the answer to this,
>> and I feel I am looking too hard. Any help would be greatly
>> appreciated.
>>
>> Thank you!
>>
>>
>>
>
> I realised that something gawky things go on in this group.
>
> This should do
> gawk '/^Data/ {last=$0}
> /^Other: 1$/{print $0 ", " last}'
>
>
I meant *some* gawky!


From: John W. Krahn on
tntelle(a)yahoo.com wrote:
> I have a ton of data, and I am trying to do something where once a
> regexp is found, it appends the last occurance before that match to a
> differnet regexp
>
> Like:
>
> Data: Something1
> Other: 1
> Something Else
> Something Else
> Data: Something2
> Other: 3
> Something Else
> Something Else
> Data: Something3
> Other: 3
> Something Else
> Something Else
> Data: Something4
> Something Else
> Something Else
> Other: 1
> Something Else
> Something Else
> Data: Something5
> Other: 1
>
>
> So if i want to find all occurances of "Other 1" Print that, and then
> append the last occurance of what "Data: X"
> So the above would become:
>
> Other: 1, Data: Something1
> Other: 1, Data: Something4
> Other: 1, Data: Something5

$ echo "Data: Something1
Other: 1
Something Else
Something Else
Data: Something2
Other: 3
Something Else
Something Else
Data: Something3
Other: 3
Something Else
Something Else
Data: Something4
Something Else
Something Else
Other: 1
Something Else
Something Else
Data: Something5
Other: 1" | perl -lne'$data = $_ if /^Data:/; print "$_, $data" if
/^Other: 1$/'
Other: 1, Data: Something1
Other: 1, Data: Something4
Other: 1, Data: Something5




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
From: Dave B on
On Wednesday 23 April 2008 00:26, tntelle(a)yahoo.com wrote:

> I have a ton of data, and I am trying to do something where once a
> regexp is found, it appends the last occurance before that match to a
> differnet regexp
>
> Like:
>
> Data: Something1
> Other: 1
> Something Else
> Something Else
> Data: Something2
> Other: 3
> Something Else
> Something Else
> Data: Something3
> Other: 3
> Something Else
> Something Else
> Data: Something4
> Something Else
> Something Else
> Other: 1
> Something Else
> Something Else
> Data: Something5
> Other: 1
>
>
> So if i want to find all occurances of "Other 1" Print that, and then
> append the last occurance of what "Data: X"
> So the above would become:
>
> Other: 1, Data: Something1
> Other: 1, Data: Something4
> Other: 1, Data: Something5
>
>
> I've been around the world and back trying to find the answer to this,
> and I feel I am looking too hard. Any help would be greatly
> appreciated.

Awk is of course fine for this, and you already have a working solution. In
case you want to use sed, here's one:

sed -n '/^Data:/{h;d};/^Other: 1$/{x;H;x;s/\n/, /;p}' yourfile

--
D.