Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

/proc/kallsyms format, sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas

Links

XODOX
Impressum

#1: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 00:26:52 by 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!

Report this message

#2: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 00:46:39 by Rajan

<tntelle@yahoo.com> 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}'

Report this message

#3: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 00:48:23 by Rajan

"Rajan" <svrajan@rediffmail.com> wrote in message
news:480e6ad4$0$90266$14726298@news.sunsite.dk...
>
>
> <tntelle@yahoo.com> 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!

Report this message

#4: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 00:51:57 by 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

Report this message

#5: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 17:10:46 by 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.

Report this message

#6: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 17:13:35 by 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.

Report this message

#7: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 17:37:06 by 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.

Report this message

#8: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 17:49:32 by 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.

Report this message

#9: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 17:57:04 by 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.

Report this message

#10: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-23 18:29:37 by 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.

Report this message

#11: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-24 00:38:12 by tntelle

On Apr 23, 11:29=A0am, Ed Morton <mor...@lsupcaemnt.com> 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!

Report this message

#12: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-24 01:59:02 by Rajan

<tntelle@yahoo.com> wrote in message
news:be22c7a9-34cd-4227-bafd-0ca9a111a72f@d1g2000hsg.googleg roups.com...
> On Apr 23, 11:29 am, Ed Morton <mor...@lsupcaemnt.com> 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

Report this message

#13: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-24 09:41:00 by 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.

Report this message

#14: Re: Matching Previous and Append with Sed / Awk

Posted on 2008-04-24 10:27:08 by Janis Papanagnou

On 24 Apr., 01:59, "Rajan" <rsha...@nodomain.no> 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

Report this message