Re: another sed question...

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



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