No need to master sed/awk/grep/wc/... use pure Ruby -- pru

all 33 comments

[–]rubygeek 5 points 1 month ago

What does this give over "ruby -n -e 'some expression'"?

[–]grosser[S] 2 points 1 month ago

an output by default, a cleaner call, helper methods for data crunching, reducing, in-file-replacement ... have a look at the readme :)

[–]absinthe718 5 points 1 month ago

I really doubt that pru will perform in the same order of magnitude in either speed or memory use when compared with sed/awk/find/grep. Some of us are old enough to have learned sed/awk/find/grep in during the Reagan administration. And we got into arguments about sed/awk/find/grep versus perl in the Clinton years. This is old hat.

[–]eternal_noob 10 points 1 month ago* 

Terrible idea. The examples are really counter-intuitive and often longer than their unix equivalent.

ps -ef | grep foo | grep -v grep
ps -ef | pru 'include?("foo") and not include?("pru")'

Second, sed, awk, et. al are useful on their own. You can't beat the convenience of awk.

cat /var/log/nginx/access_log |awk '$9 ~ 404'

It is more useful to learn the "regular" ruby equivalent instead of an obscure script. See:

ls -al | pru 'gsub(/5/,"five")'
ls -al | ruby -pe 'gsub(/5/, "five")'

Or the line count example:

ls -al | pru --reduce 'size'
ls -al | ruby -ne 'END {puts $.}'

You can't count on rubygems being present either, but short of windows sed, awk and friends is.

[–]grosser[S] 3 points 1 month ago

pru is targeted at people that know ruby and do not want to learn x new languages of their own just to be able to collect/format data. If you know/like all those tools pru sure seems redundant. But if you do not know them or do not like them pru can be a simple way to do things that would normally require a new script file / irb session.

It shines when:
- examples get more complex
- you do not know the alternative sed/awk/xxx syntax
- you do not want to learn how each regex flavor is different in tiny ways / needs to be escaped a bit different

ruby -pe can do a lot of stuff pru can do, but pru adds many helpers that get handy when crunching data like group_by / sort_by / grouped / mean / aility to reduce and many more

[–]seydar 0 points 1 month ago

Just admit that you made this for fun and learning and be done with it. It really can't beat sed/awk/grep in any fashion.

[–]weavejester 1 point 1 month ago

I'm actually inclined to agree with grosser here. I don't need awk or sed often enough to remember all their syntax from memory, so using pru might work out to be faster for me than the classic awk/sed/grep combination. I'd need to try it out to be certain, however.

[–]Baramin 9 points 1 month ago

I wouldn't recommend learning 'pru' instead of the classic tools, as you'll never find pru installed on a unix server anywhere, you'll have to install it yourself, meaning installing ruby first, which can be a big problem on enterprise servers.

[–]mwilden 2 points 1 month ago

It's a good point. Non-Windows programmers simply have to learn the common Unix commandline tools as part of their job. pru is well-intentioned, but I think it will actually hurt those who use it. Like it or not, we need to know how to use grep and sed.

[–]est3est 1 point 1 month ago

I actually like the idea, if the goal is just to make my work done faster. If you don't like the idea of making things easier and quicker then why would you use languages like Python or Ruby, when there is already C. But to be honest, I found examples a bit confusing at the moments. Still, this have a potential, and I am very interested where it will go...

[–]alterlaszlo999 4 points 1 month ago

that' what i dislike in ruby community: it seems they always reinvent the wheel 'cause they have only ONE dogma. and in a way they disvalue the unix under their bottom.

[–]est3est 2 points 1 month ago

Good luck with your attitude.

[–]alterlaszlo999 0 points 1 month ago

i've been quite rude, i know, and I appreciate ruby comm. in many ways; but two more examples:

'whenever' instead of learning 'cron' syntax

'god' instead of for ex. upstart

[–]joedirt123 1 point 1 month ago

'whenever' instead of learning 'cron' syntax

You can use cron syntax in whenever if you want.

'god' instead of for ex. upstart

God came before upstart. Upstart is not used by all linux distros. God works on Macs.

[–]jyper 2 points 1 month ago

For a while it did look like most of the major distros were adopting it(although I doubt arch/gentoo/slackware would ever use it) but now a bunch of them are switching to systemd with ubuntu sticking with upstart for now(and I'm not sure about debian).

[–]joedirt123 1 point 1 month ago

I also forgot to mention that you need to be root manage those types of systems.

[–]rgh 1 point 1 month ago

The Debian guys are discussing systemd but it's made hard by the fact they now support FreeBSD. systemd only works with Linux and Poettering has said he has no intention of supporting anything else.

[–]est3est 1 point 1 month ago

Now, we can talk. I don't know.., 'god' sounds so much better than f.i. upstart. Enough of jokes, those gems for better or for worse, try to make things just a little bit simpler. This doesn't mean they don't value unix tools. It's your choice. And honestly, you didn't mention rake, another wheel reinvention. Would you really like to use make instead of rake in all your projects? I don't see anything bad in that kind "inventions", you just got more variety of wheels to chose from, which supposed to be a good thing.

[–]GSpotAssassin 1 point 1 month ago

Whenever is really nice and even though I know Cron syntax, I prefer it for readability and codeability. Also, the real advantage of Whenever is making your scheduled jobs part of your code repo instead of having to manage a crontab file external to your repo.

[–]grosser[S] 4 points 1 month ago

pru tries to bring the unix way (piping stuff around) to rubyists, if you mastered sed/awk/... then this may be unnecessary, but if you are a poor fella that only knows his ruby, it can make your life easier

[–]seydar 2 points 1 month ago

oh hoorary let's generalize

[–]_random_guy_ 0 points 1 month ago

Great job, kudos for that. But, there are a lot of reason why this is not a good idea - ruby is slow, for instance.

Also, if you look at the examples you've written yourself, they are less concise than the grep equivalent.

[–]grosser[S] 2 points 1 month ago

most of the examples are longer then the original, since making specialized stuff in a general-purpose language will mostly be longer then in a specialized tool. Pru is not about stuff like "grep foo | grep -v grep", but about e.g.
listing all your repos sorted by watches on github ...

`ls | pru '[self, `curl --silent "https://github.com/grosser/#{self}"`.match(/<li class="watchers.*?(\d+)\s*<\/a/m)[1].to_i] rescue nil' 'sort_by(&:last).reverse.map{|n,i| "#{n} => #{i}" }'`

its kind of messy, but readable and I wrote it in 3min tops and I have no idea how to do this with awk/sed/grep/xxx :>

[–]mwilden 3 points 1 month ago

I've said this before, and I'll keep saying it: When writing documentation, use real examples. Real examples may take more time for us readers to understand, but they'll actually help us use the tool, and, more importantly, show how valuable the tool is.

[–]_random_guy_ 1 point 1 month ago

Honestly, if you want to do something as complex as that, you're better off writing a full-fledged script and then execute it (ruby script.rb). It makes no sense to do something as complex as this with a one liner, with either grep or ruby.

[–]est3est 1 point 1 month ago

Honestly I can't tell the difference in terms of speed.

[–]_random_guy_ 1 point 1 month ago

Try with something heavy, like grepping for "te" on the complete contents of your hard drive.

[–]rgh 2 points 1 month ago

yeah, 'cause I do that everyday. Usually before breakfast, it helps me relax knowing which files contain te.

[–]_random_guy_ 1 point 1 month ago

Not so unusual for me to grep my whole filesystem for .sql files. Just sayin'.

[–]projecteternity 2 points 1 month ago

Fun fact, ruby regex matching is orders of magnitude slower than sed/awk/grep. The proof is kind of depressing.

[–]GSpotAssassin 1 point 1 month ago

This is for Ruby 1.8.4. It is my understanding that Ruby >1.9 has some significant optimizations.

[–]rgh 2 points 1 month ago

Having used grep/sed/awk/... for the last 15 years or so I actually think this is a nice idea.

As a ruby developer and sysadm and I think there's a place for both. You have to remember that not everyone is going to master sed and awk. As incredibly powerful as they are the syntax is arcane (that's no necessarily a bad thing but if you don't use them regularly they can be hard to remember). So I suspect that this find a sweet spot with ruby devs.

I for one will be adding to my toolkit.

[–]bigfig 1 point 1 month ago

Inline perl (perl -pe "s/\w//" etc) often does what sed won't (easily), but I still use sed. Awk never suited me as it seems to fill exactly the same niche as perl, and grep /egrep will remain go-to tools for parsing logs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值