Re: [CentOS] another sed question...

2008-05-31 Thread Filipe Brandenburger
On Fri, May 30, 2008 at 12:33 PM, Craig White <[EMAIL PROTECTED]> 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


Re: [CentOS] another sed question...

2008-05-30 Thread Thomas Johansson

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


Re: [CentOS] another sed question...

2008-05-30 Thread Craig White
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


Re: [CentOS] another sed question...

2008-05-30 Thread Thomas Johansson

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


Re: [CentOS] another sed question...

2008-05-30 Thread MHR
On Fri, May 30, 2008 at 9:33 AM, Craig White <[EMAIL PROTECTED]> 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


[CentOS] another sed question...

2008-05-30 Thread Craig White
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