Matching Previous and Append with Sed / Awk

Matching Previous and Append with Sed / Awk

am 23.04.2008 00:26:52 von tntelle

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!

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 00:46:39 von Rajan

wrote in message
news:533eb791-ad84-4cd7-b4ff-2bdc7ca98e17@k37g2000hsf.google groups.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}'

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 00:48:23 von Rajan

"Rajan" wrote in message
news:480e6ad4$0$90266$14726298@news.sunsite.dk...
>
>
> wrote in message
> news:533eb791-ad84-4cd7-b4ff-2bdc7ca98e17@k37g2000hsf.google groups.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!

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 00:51:57 von someone

tntelle@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

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 17:10:46 von Dave B

On Wednesday 23 April 2008 00:26, tntelle@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.

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 17:13:35 von Dave B

On Wednesday 23 April 2008 17:10, Dave B wrote:

> Awk is of course fine for this

And perl too, of course (sorry, I overlooked that reply).

--
D.

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 17:37:06 von Dave B

On Wednesday 23 April 2008 17:10, Dave B wrote:

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

This can be further simplified, changing the x:H:x to G:

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

--
D.

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 17:49:32 von Ed Morton

On 4/23/2008 10:37 AM, Dave B wrote:
> On Wednesday 23 April 2008 17:10, Dave B wrote:
>
>
>>sed -n '/^Data:/{h;d};/^Other: 1$/{x;H;x;s/\n/, /;p}' yourfile
>
>
> This can be further simplified, changing the x:H:x to G:
>
> sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /;p}' yourfile
>

and can be simplified even further to:

awk '/^Data:/{d=$0} /^Other: 1$/{print $0", "d}' yourfile

;-). The point is that for clarity you shouldn't use sed for anything other than
simple substituions on one line.

Ed.

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 17:57:04 von Dave B

On Wednesday 23 April 2008 17:49, Ed Morton wrote:

>> sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /;p}' yourfile
> awk '/^Data:/{d=$0} /^Other: 1$/{print $0", "d}' yourfile

Argh, 1 character less :-)

> The point is that for clarity you shouldn't use sed for anything
> other than simple substituions on one line.

Well, that depends on who's judging the "clarity" :-)
Seriously, I agree with you for the case of long and complex sed scripts. In
this case, it looked clear and simple enough (to me, at least!) to be worth
posting it, also considering that the OP might want/like a sed solution
too.

--
D.

Re: Matching Previous and Append with Sed / Awk

am 23.04.2008 18:29:37 von Ed Morton

On 4/23/2008 10:57 AM, Dave B wrote:
> On Wednesday 23 April 2008 17:49, Ed Morton wrote:
>
>
>>>sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /;p}' yourfile
>>
>> awk '/^Data:/{d=$0} /^Other: 1$/{print $0", "d}' yourfile
>
>
> Argh, 1 character less :-)
>
>
>>The point is that for clarity you shouldn't use sed for anything
>>other than simple substituions on one line.
>
>
> Well, that depends on who's judging the "clarity" :-)

Let's assume they're sane ;-).

> Seriously, I agree with you for the case of long and complex sed scripts. In
> this case, it looked clear and simple enough (to me, at least!) to be worth
> posting it,

I know, but all those h's, d's, G's, p's, etc. you need in sed scripts to do the
simplest things with multi-line records just seem so convoluted compared to just
having a variable and some words (instead of characters) for commands, and it
doesn't even save you much, if any, typing....

> also considering that the OP might want/like a sed solution too.

That can be true, usually when they already know sed and think they can/should
just keep building on it for more complex problems rather than learning/using a
different tool that'll make their lives easier almost immediately. I think
pointing out how simple things are with appropriate tools is more useful than
telling them how to make it work with sed but it is also useful to have a sed
example to refer to!

Ed.

Re: Matching Previous and Append with Sed / Awk

am 24.04.2008 00:38:12 von tntelle

On Apr 23, 11:29=A0am, Ed Morton wrote:
> On 4/23/2008 10:57 AM, Dave B wrote:
>
>
>
>
>
> > On Wednesday 23 April 2008 17:49, Ed Morton wrote:
>
> >>>sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /;p}' yourfile
>
> >> awk '/^Data:/{d=3D$0} /^Other: 1$/{print $0", "d}' yourfile
>
> > Argh, 1 character less :-)
>
> >>The point is that for clarity you shouldn't use sed for anything
> >>other than simple substituions on one line.
>
> > Well, that depends on who's judging the "clarity" :-)
>
> Let's assume they're sane ;-).
>
> > Seriously, I agree with you for the case of long and complex sed scripts=
.. In
> > this case, it looked clear and simple enough (to me, at least!) to be wo=
rth
> > posting it,
>
> I know, but all those h's, d's, G's, p's, etc. you need in sed scripts to =
do the
> simplest things with multi-line records just seem so convoluted compared t=
o just
> having a variable and some words (instead of characters) for commands, and=
it
> doesn't even save you much, if any, typing....
>
> > =A0also considering that the OP might want/like a sed solution too.
>
> That can be true, usually when they already know sed and think they can/sh=
ould
> just keep building on it for more complex problems rather than learning/us=
ing a
> different tool that'll make their lives easier almost immediately. I think=

> pointing out how simple things are with appropriate tools is more useful t=
han
> telling them how to make it work with sed but it is also useful to have a =
sed
> example to refer to!
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -

Thank you all for the help!!
Saddly.. i've tried all of the examples and none work =3D/

I either get nothing back or, Function /^Data:/{h;d};/^Other: 1$/{G;s/
\n/, /;p} cannot be parsed.

Hopefully I missed something simple..... Thanks!

Re: Matching Previous and Append with Sed / Awk

am 24.04.2008 01:59:02 von Rajan

wrote in message
news:be22c7a9-34cd-4227-bafd-0ca9a111a72f@d1g2000hsg.googleg roups.com...
> On Apr 23, 11:29 am, Ed Morton wrote:
>> On 4/23/2008 10:57 AM, Dave B wrote:
>>
>>
>>
>>
>>
>> > On Wednesday 23 April 2008 17:49, Ed Morton wrote:
>>
>> >>>sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /;p}' yourfile
>>
>> >> awk '/^Data:/{d=$0} /^Other: 1$/{print $0", "d}' yourfile
>>
>> > Argh, 1 character less :-)
>>
>> >>The point is that for clarity you shouldn't use sed for anything
>> >>other than simple substituions on one line.
>>
>> > Well, that depends on who's judging the "clarity" :-)
>>
>> Let's assume they're sane ;-).
>>
>> > Seriously, I agree with you for the case of long and complex sed
>> > scripts. In
>> > this case, it looked clear and simple enough (to me, at least!) to be
>> > worth
>> > posting it,
>>
>> I know, but all those h's, d's, G's, p's, etc. you need in sed scripts to
>> do the
>> simplest things with multi-line records just seem so convoluted compared
>> to just
>> having a variable and some words (instead of characters) for commands,
>> and it
>> doesn't even save you much, if any, typing....
>>
>> > also considering that the OP might want/like a sed solution too.
>>
>> That can be true, usually when they already know sed and think they
>> can/should
>> just keep building on it for more complex problems rather than
>> learning/using a
>> different tool that'll make their lives easier almost immediately. I
>> think
>> pointing out how simple things are with appropriate tools is more useful
>> than
>> telling them how to make it work with sed but it is also useful to have a
>> sed
>> example to refer to!
>>
>> Ed.- Hide quoted text -
>>
>> - Show quoted text -
>
> Thank you all for the help!!
> Saddly.. i've tried all of the examples and none work =/
>
> I either get nothing back or, Function /^Data:/{h;d};/^Other: 1$/{G;s/
> \n/, /;p} cannot be parsed.
>
> Hopefully I missed something simple..... Thanks!

This is what I see. Can you show something similar?
$cat newfile
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
$gawk '/^Data/{d=$0} /^Other: 1$/{print $0 ", " d}' new.txt
Other: 1, Data: Something1
Other: 1, Data: Something4
Other: 1, Data: Something5

Re: Matching Previous and Append with Sed / Awk

am 24.04.2008 09:41:00 von PK

On Thursday 24 April 2008 00:38, tntelle@yahoo.com wrote:

> Thank you all for the help!!
> Saddly.. i've tried all of the examples and none work =/
>
> I either get nothing back or, Function /^Data:/{h;d};/^Other: 1$/{G;s/
> \n/, /;p} cannot be parsed.
>
> Hopefully I missed something simple..... Thanks!

Post the *exact* commands you executed (better if you do copy/paste), and
the *exact* input you have (or at least, some lines).

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: Matching Previous and Append with Sed / Awk

am 24.04.2008 10:27:08 von Janis Papanagnou

On 24 Apr., 01:59, "Rajan" wrote:
>
> > Hopefully I missed something simple..... Thanks!

Maybe; but don't expect the audience here to _guess_
what *you* have done so that "none worked" for you.

>
> This is what I see. Can you show something similar?

Hopefully not! ;-)

> $cat newfile
> [...]
> $gawk '/^Data/{d=$0} /^Other: 1$/{print $0 ", " d}' new.txt
> [...]

Only if the files have the same contents.

Janis