|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
<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}' |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"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! |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Wednesday 23 April 2008 17:10, Dave B wrote:
> sed -n '/^Data:/{h;d};/^Other: 1$/{x;H;x;s/\n/, / }' yourfileThis can be further simplified, changing the x:H:x to G: sed -n '/^Data:/{h;d};/^Other: 1$/{G;s/\n/, / }' yourfile-- D. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
<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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
"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. |
|
![]() |
| Outils de la discussion | |
|
|