[CentOS] another sed question...

Fri May 30 19:37:45 UTC 2008
Thomas Johansson <thomasj at isy.liu.se>

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 at 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))