Re: another sed question...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Fri, May 30, 2008 at 9:33 AM, Craig White <craigwhite@xxxxxxxxxxx> 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@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux