[CentOS] another sed question...

Sat May 31 13:16:20 UTC 2008
Filipe Brandenburger <filbranden at gmail.com>

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