FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 05-30-2008, 04:33 PM
Craig White
 
Default another sed question...

Looking to change a yml file (yaml is a database type file)

*** from
--- !ruby/object:Right
attributes:
name: Personnel Admin
action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes:
name: Personnel Admin
action: find
id: "2"
controller: assessments

*** to
--- !ruby/object:Right
attributes_1:
name: Personnel Admin
action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes_2:
name: Personnel Admin
action: find
id: "2"
controller: assessments

where I'm taking the 'id:' field from each record and inserting an
underscore and the id into the 'attributes' label directly above.

Any takers?

Thanks

Craig

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 05-30-2008, 06:21 PM
MHR
 
Default another sed question...

On Fri, May 30, 2008 at 9:33 AM, Craig White <craigwhite@azapple.com> wrote:
>
> Looking to change a yml file (yaml is a database type file)
>
> *** from
> --- !ruby/object:Right
> attributes:
> name: Personnel Admin
> action: index
> id: "1"
> controller: assessments
> --- !ruby/object:Right
> attributes:
> name: Personnel Admin
> action: find
> id: "2"
> controller: assessments
>
> *** to
> --- !ruby/object:Right
> attributes_1:
> name: Personnel Admin
> action: index
> id: "1"
> controller: assessments
> --- !ruby/object:Right
> attributes_2:
> name: Personnel Admin
> action: find
> id: "2"
> controller: assessments
>
> where I'm taking the 'id:' field from each record and inserting an
> underscore and the id into the 'attributes' label directly above.
>
> Any takers?
>

Sed is not a good candidate for this, although you might be able to do
it with some seriously twisted script. Sed is a stream editor - its
commands are applied to every line it sees.

Awk is a much better candidate for this - you could write a not
terribly difficult script to cache the lines up to the id, modify the
attributes line and spit out the whole batch. As for writing it for
you, I strongly urge you to read the man page and see if you can't do
one yourself. It will be worth the effort in future endeavors.

Once I taught myself how to write in awk, I found 1) reading it in
other people's scripts was a WHALE of a lot easier and 2) it wasn't
that hard to write anything from simple ones to really complicated
ones.

However, really complicated awk scripts are of limited use IMNSHO
because a really complicated pattern transformation that needs to be
executed a lot is better implemented in a program (i.e., C, C++ or
whatever) or a more advanced scripting language like Perl (or Python,
which I've yet to use).

You could even do this in a shell script....

mhr
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 05-30-2008, 06:26 PM
Thomas Johansson
 
Default another sed question...

Craig White wrote:

Looking to change a yml file (yaml is a database type file)

*** from
--- !ruby/object:Right
attributes:
name: Personnel Admin

action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes:
name: Personnel Admin

action: find
id: "2"
controller: assessments

*** to
--- !ruby/object:Right
attributes_1:
name: Personnel Admin

action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes_2:
name: Personnel Admin

action: find
id: "2"
controller: assessments

where I'm taking the 'id:' field from each record and inserting an
underscore and the id into the 'attributes' label directly above.

Any takers?

Thanks

Craig

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

>
I use awk instead of sed...

Reverse file order, perform changes using awk and reverse again

tac test.yml | awk '{if ($1=="id:"){idtag=substr($2,2,1)};
if ($1=="attributes:") {print "attributes_"idtag":"}
else {print $0}}' | tac -
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 05-30-2008, 07:25 PM
Craig White
 
Default another sed question...

On Fri, 2008-05-30 at 20:26 +0200, Thomas Johansson wrote:
> Craig White wrote:
> > Looking to change a yml file (yaml is a database type file)
> >
> > *** from
> > --- !ruby/object:Right
> > attributes:
> > name: Personnel Admin
> > action: index
> > id: "1"
> > controller: assessments
> > --- !ruby/object:Right
> > attributes:
> > name: Personnel Admin
> > action: find
> > id: "2"
> > controller: assessments
> >
> > *** to
> > --- !ruby/object:Right
> > attributes_1:
> > name: Personnel Admin
> > action: index
> > id: "1"
> > controller: assessments
> > --- !ruby/object:Right
> > attributes_2:
> > name: Personnel Admin
> > action: find
> > id: "2"
> > controller: assessments
> >
> > where I'm taking the 'id:' field from each record and inserting an
> > underscore and the id into the 'attributes' label directly above.
> >
> > Any takers?
> >
> > Thanks
> >
> > Craig
> >
> > _______________________________________________
> > CentOS mailing list
> > CentOS@centos.org
> > http://lists.centos.org/mailman/listinfo/centos
> >
> I use awk instead of sed...
>
> Reverse file order, perform changes using awk and reverse again
>
> tac test.yml | awk '{if ($1=="id:"){idtag=substr($2,2,1)};
> if ($1=="attributes:") {print "attributes_"idtag":"}
> else {print $0}}' | tac -
----
That was awesome...thanks...I learned a lot there.

I had to adjust the length of the substr function to get what I needed.

Thanks

Craig

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 05-30-2008, 07:37 PM
Thomas Johansson
 
Default another sed question...

Craig White wrote:

On Fri, 2008-05-30 at 20:26 +0200, Thomas Johansson wrote:

Craig White wrote:

Looking to change a yml file (yaml is a database type file)

*** from
--- !ruby/object:Right
attributes:
name: Personnel Admin

action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes:
name: Personnel Admin

action: find
id: "2"
controller: assessments

*** to
--- !ruby/object:Right
attributes_1:
name: Personnel Admin

action: index
id: "1"
controller: assessments
--- !ruby/object:Right
attributes_2:
name: Personnel Admin

action: find
id: "2"
controller: assessments

where I'm taking the 'id:' field from each record and inserting an
underscore and the id into the 'attributes' label directly above.

Any takers?

Thanks

Craig

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

>
I use awk instead of sed...

Reverse file order, perform changes using awk and reverse again

tac test.yml | awk '{if ($1=="id:"){idtag=substr($2,2,1)};
if ($1=="attributes:") {print "attributes_"idtag":"}
else {print $0}}' | tac -

----
That was awesome...thanks...I learned a lot there.

I had to adjust the length of the substr function to get what I needed.

Thanks

Craig

I didn't think of numbers larger than 9 :-/ Here is an upgraded version
of my script.. modify the substr command slightly.. That will enable
numbers up to 10 chars long.


idtag=strtonum(substr($2,2,10))
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 05-31-2008, 01:16 PM
"Filipe Brandenburger"
 
Default another sed question...

On Fri, May 30, 2008 at 12:33 PM, Craig White <craigwhite@azapple.com> wrote:
> where I'm taking the 'id:' field from each record and inserting an
> underscore and the id into the 'attributes' label directly above.

Just for fun, this is a one-line sed script that would change that file:

sed -n -e '/^attributes:$/{' -e 'n' -e 'h' -e 's/^ id:
"(.*)"$/attributes_1:/' -e 'Ta' -e 'G' -e 'p' -e 'b' -e ':a' -e 'n'
-e 'H' -e 's/^ id: "(.*)"$/attributes_1:/' -e 'Ta' -e 'G' -e '}' -e
'p'

It could probably be done better than that.

"sed" can do anything (there is an example that implements the "bc"
calculator in "sed"), but it's certainly not the best tool for
anything. These days, I would say that foranything that involves
correlating lines (actually, anything that involves more than
substitutions and deletion of lines -- s/// and //d) you would be
better off with perl or python.

I wouldn't bother learning awk, if you want to spend your time
learning something, go directly to perl or python. awk tends to get
very ugly when your script grows, and it does many things in an
AWKward way.

For text processing, Perl is still king. Python can certainly be used
for that, but even though I know Python well, for tasks such as the
one above I would choose Perl. The way regular expressions are
embedded in the language makes it very productive to work with these
problems.

HTH!
Filipe
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 

Thread Tools




All times are GMT. The time now is 05:59 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org