[CentOS] another sed question...

MHR mhullrich at gmail.com
Fri May 30 18:21:08 UTC 2008


On Fri, May 30, 2008 at 9:33 AM, Craig White <craigwhite at 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


More information about the CentOS mailing list