PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Matching Previous and Append with Sed / Awk
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Matching Previous and Append with Sed / Awk

Réponse
 
LinkBack Outils de la discussion
Vieux 22/04/2008, 23h26   #1
tntelle@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Matching Previous and Append with Sed / Awk

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 would be greatly
appreciated.

Thank you!



  Réponse avec citation
Vieux 22/04/2008, 23h46   #2
Rajan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



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


  Réponse avec citation
Vieux 22/04/2008, 23h48   #3
Rajan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



"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.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 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!


  Réponse avec citation
Vieux 22/04/2008, 23h51   #4
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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
  Réponse avec citation
Vieux 23/04/2008, 16h10   #5
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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 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/, /}' yourfile

--
D.
  Réponse avec citation
Vieux 23/04/2008, 16h13   #6
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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.
  Réponse avec citation
Vieux 23/04/2008, 16h37   #7
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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

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


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

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

--
D.
  Réponse avec citation
Vieux 23/04/2008, 16h49   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



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/, /}' yourfile

>
>
> This can be further simplified, changing the x:H:x to G:
>
> sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /}' 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.


  Réponse avec citation
Vieux 23/04/2008, 16h57   #9
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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

>> sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, /}' 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.
  Réponse avec citation
Vieux 23/04/2008, 17h29   #10
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



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/, /}' 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.

  Réponse avec citation
Vieux 23/04/2008, 23h38   #11
tntelle@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

On Apr 23, 11:29am, 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/, /}' 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, andit
> 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 !!
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/, /} cannot be parsed.

Hopefully I missed something simple..... Thanks!
  Réponse avec citation
Vieux 24/04/2008, 00h59   #12
Rajan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



<tntelle@yahoo.com> wrote in message
news:be22c7a9-34cd-4227-bafd-0ca9a111a72f@d1g2000hsg.googlegroups.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/, /}' 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 !!
> 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/, /} 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

  Réponse avec citation
Vieux 24/04/2008, 08h41   #13
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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

> Thank you all for the !!
> 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/, /} 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.
  Réponse avec citation
Vieux 24/04/2008, 09h27   #14
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk

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
  Réponse avec citation
Vieux 24/04/2008, 23h40   #15
Rajan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Matching Previous and Append with Sed / Awk



"Janis" <janis_papanagnou@hotmail.com> wrote in message
news:b9d0a050-3d94-4b6d-ba1f-7e03862cb9f9@w74g2000hsh.googlegroups.com...
> 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


My mistake both are same! The first was created when I wrote my last= the
second, I created again before running Ed's d=. I just wanted to run it
again for pasting the output. Sorry about that.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,29215 seconds with 23 queries