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