views:

255

answers:

4

I want to learn Sed. Can you please point me to good references so that I can fully utilize it.

I want to learn it to perform more of the do-once-then-forget type administrative or dev-tools like tasks. So, I don't really care about performance or modularity or object orientedness etc when writing this type of code. Do you think it would be a good idea to learn Sed? Or should I learn Ruby? My main concern is the conciseness of scripts, but not to the point of making it totally obscure looking piece of code.

Thanks Ajay

+1  A: 

I can't think of anything that sed is better at than ruby. Sed's syntax for a lot of things is very similar to Ruby sub and gsub string methods. I guess I would point you to Ruby unless you really have no other use for a scripting language other than text processing. You could probably get a reasonable understanding of how to use sed a little quicker than how to do the same stuff in ruby.

Joe Cannatti
Can I write pithy one-liners and run the ruby code directly from the command prompt without writing it into a file and then running that file?
ajay
Ruby has a toplevel, or you can pipe the input into it.echo puts 'Hello world' | ruby ( on windows -- Linux handles this better)
Joel
ruby -e "puts 'hello'"
frou
@ajay: ruby -e does that for you. example: ruby -e '10.times do |t| puts t end'For parsing streams you can also do stuff like echo "foobarbaz" | ruby -pe 'gsub(%r{foo}, "bar")'
I would add that it's good to at least know how to do the basics, like gsub, in sed: Sometimes you will end up on a system with the normal Unix tools but without Ruby.
Wayne Conrad
A: 

I would encourage you to take a look at Perl for tasks like this. Many of Perl's features are designed expressly with the goal of "processing text with concise syntax". It can of course jump over into cryptic, but so can Ruby.

Perl also has extensive support for one line programs typed into the shell. echo "hello world" | perl -lpe 's/\b(.)/\U$1/g' prints Hello World

Eric Strom
A: 

Forget sed, anything more than a simple, line-oriented search and replace is just unreadable. Learn Awk. And then learn Ruby too: it's good for your soul.

glenn jackman