Show HN: Nip – Use JavaScript instead of awk, sed, or grep
github.com
github.com
The author writes "this is for people who aren't 'devops' and who can't crank out a fancy piped shell script using awk and sed."
Whatever we may feel about people who have inexplicably failed to learn to use awk or sed, or perl, or python, or ruby, criticism on the basis of these methods being "better" (if only one learns how to use them) is a waste of everyone's time.
If you really want to make a point about how less performant javascript is than perl, that's a different matter. If you want to critique the goals, concept, implimentation, or whatever, that's a different matter.
But it's simply too easy to write "why not do 'X'", especially if you're going to ignore the fact that the constraints on the problem have already ruled out 'X' in the first place. That takes less than no thought. It takes an active deficit of thought.
Reinventing every wheel "because $my_pet_language" is fractious, generally worse than the alternative, and creates fairly unproductive noise--I think it's reasonable that people should catch at least a little flak for doing it. I mean, if criticizing anti-cooperative behavior is a waste of time, what was engaging in it? The idea that bash and GNU coreutils are "devops" tools, instead of the tools of basically competent Unix-based software developers, is so ludicrous as to be worth a little roasting. (And it has been a little. Nobody here, not even me in this post, is being more than mild.)
HN doesn't go for gratuitous negativity, and that's pretty cool, but if this thread's any measuring stick, negativity at this sort of behavior is a long way from gratuitous.
Yeah sure if you mastered perl, grep/awk/sed and can whip up what you need in a few seconds, more power to you, but I'm not going to waste an hour of my time googling and reading man pages when I can take 3 seconds to write a quick function in JavaScript to do what I need for that one task.
Polluting the global namespace with yet another tool that's worse at its job than the existing ones because of a particular breed of aggressive incuriosity and epistemic closure is real weird. (This isn't a failing unique to the JavaScript community. Go people love doing this, too, creating bad solutions to solved problems. You used to see it a lot in Ruby, but the community seems to have grown up a little.)
My anecdote: most "Go people" doing this are actually "Go beginners" that are still learning the language and attempting to write something familiar to cement their understanding. I imagine this is often the case for other languages.
I'll admit, I've written a few of these myself to help nail down both my knowledge of Go and the basic toolset I use every day. It was quite useful as a learning tool, even though they'll never replace the originals.
Heck, I'd even recommend re-creating `dc` to anybody who is interested in learning a language, or writing a parser/interpreter for a language.
This is what I was referring to, yeah, and it is to my mind a sign of a certain amount of cultural insularity and maybe a little hubris. Reimplementing something like `dc` to learn a language is great, it's when you start movements to start replacing things--or even trying to be taken credibly as an alternative--that I think you had best come very correct or be ready to defend your contention that the status quo is for some reason unsuitable. "I don't already know sed" is not, to my mind, a good defense, which touched off this subthread.
Also gnu awk, sed, and grep are not the same as BSD variants. The gnu versions have (some would say suffer from) a lot more features.
For me, anyone who uses unix and doesn't have working knowledge of how to use find, xargs, sed, awk, and grep is missing out on a lot and very likely wasting time reinventing wheels.
I use the GNU tools for consistency, as it's easier to install them on a BSD system than the reverse, but your point is well-made.
Let's look at it briefly from the flip side. I'm in Devops myself, and I am quite confident chaining together seds, awks, and greps. Those didn't help me when I had to troubleshoot a Node application, though. What did? I also learned enough about Javascript to be able to not only navigate code, but debug what was happening.
Artifically limiting yourself by not taking these opportunities to learn about an OS you seem to spend a fair bit of time in is only hurting you.
Heck, awk is two generations removed. Even Perl one-liners are fading into history.
I'm not sure I would characterize it as a "because my pet language" tool; rather, it allow one's main production language to use shell pipes, which can be quite handy for a variety of uses.
...
As a developer who, while (I like to think) competent, hasn't learned to use sed/awk very well .. I find it aggravating and at times arrogant that long-time users scoff at the idea of replacing them with more general and accessible tools. Why should I have to learn an esoteric syntax with loads of historical baggage and obtuse documentation(1), just because you put up with it?
(1) man pages suck, sorry. Good tool documentation starts with basic examples. If I just need to delete a line, search a file, or uncompress a tarball, I should be able to find the simple command in O(1) lines read.
...
Case in point, there's a comment here:
"Wait: nip 'function(l) { return /^var/.test(l); }' is supposed to be better than egrep '^var' ?"
Yes, it absolutely is better (except when performance really matters), because it's very easy to see how to extend that to more complex things, whereas I don't know how to extend egrep without doing lots of reading - if it's even possible without switching tools. And because I can trivially wrap the former in a shell script to produce the latter, more or less.
...
I feel this way about a lot of things. I think some people who 'grew up with' certain older technologies end up saying everyone else should use them (which may be true because they're omnipresent) and then extend that logic to say that they're actually good (which is not necessarily and often not true).
Though the example in question, you're putting in a huge amount of boilerplate code that you're going to have to write a wrapper for with each new script. Most of the basic tools -- sed, grep, awk, even perl -- tend to have a quick and dirty form that does what most people need, with the more powerful portions at the ready. The "new and improved" tools tend to end up a lot like your examples, and get swiftly ignored because it becomes a pain in the ass to type the same thing over and over again, or they grow shortcuts and secrets as time goes on until they too are full of history and shortcuts that only the old timers know. Someone's going to eventually want a patch for a command line flag that wraps all that boilerplate in, and you're going to end up with perl's -n flag, and the horribly ugly code of
perl -ne '/^var/ and print'
which will confuse beginners, and be a godsend to veterans.You get the angry DBA coming over with a bat to smack sense into you because You Do Not Work On Servers.
No. Guides and tutorials do that. Documentations teaches how to use a tool, so you can understand it and fully utilize it.
For snippets, bit of working scripts, etc, you have how-to guides, and alike on the internet.
The documentation is targeted towards someone wanting to learn about the tool; not somebody who just wants to use it once.
It's especially better you lower the barrier to entry into the world of command-line-fu. There's a huge class of people in the world who will never, ever touch a command line tool - it's the "non-DevOps" people in the article. Or you can just call them 'non-engineers'.
I don't blame these people for avoiding them, either, because ours tools are actively hostile to new users. (in my opinion) The linux community frequently takes a stance of being actively hostile to new users, by intentionally making simple tasks difficult and refusing to provide the most basic of hand-holding.
And, of course you can look these things up on the internet, but by that argument we should do away with local man pages entirely. Nah, I'd rather just make the local documentation better.
Why is moving examples from the bottom to the top better?
> There's a huge class of people in the world who will never, ever touch a command line tool
So that's your argument in defence of a new command line tool? Oh, the irony.
Also, how many of those non-cli users actually know enough programming so as to write raw JavaScript on the cli?
I guess I shouldn't have mentioned the people who never touch command-line tools. It's the people who almost-never do who are more convertible. Anyway, there's a marked difference between an easy way to run code on the command line, and a special-purpose tool with special-purpose syntax. Those non-advanced users are the ones who would benefit from having something with easily composable parts. (this is the same argument for building lots of small CLI tools and then composing them together, taken further?)
I wager lots of amateur web developers can manage their way around basic JavaScript. It used to be people started coding with shell hacking so they were forced to learn the bizarre tool suite. Now they probably start with web development [citation needed].
Because you program in JS all day long and don't want to look up man pages every time you want to do some simple text processing, that's why.
If you learn sed and awk you can slice and dice text damn near everywhere. Nip is only going to be useful where you have enough control to install it.
But for those of us for whom it's the other way round, it makes far more sense to use the scripting language we already know for the occasional text processing task than to spend time learning another language for the purpose.
Look up man pages or Google Stack Overflow answers for 15 minutes to do what I need?
Or I can just use nip and write a quick snippet that does what I know and move onto what's most important, the task I was trying to accomplish.
In the end, nobody cares how I renamed all the images from lolimag_{i}.jpg to 2015_05_20_img_{i}.jpg, they just care that I did it.
But I am as green as it gets when it comes to Javascript.
I am sometimes forced to use a Windows workstation.
So I installed "super sed" (ssed.exe), a Windows sed, from sed.sourceforge.net.
It is painfully slow. Ridiculously slow.
I'd like to try using Javascript to do some sed-style editing on text that comes through the browser.
Let's say I open up the Javascript console in a browser, e.g., Chrome.
I know little of Javascript but I do know about window.location() and XMLHttpRequest().
Assuming I can get the text I want to edit into the browser window... what do I do next?
var text = `Hello World:
Here is a multiline
piece of text`
I would then follow this guide on JavaScript regex:https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/R...
Here's a short example/sample, based on the text above: (You should be able to copy-paste this directly into the console as well, following the declaration of `text` above)
// Logs the lines to console that match a certain regex.
// This is a very poor approximation of grep.
var re = /(Hello|text)/g
text.split("\n").forEach(function(line) {
if (line.match(re)) {
console.log(line);
}
});
Using the JavaScript console of your browser is a great way to learn the language, prototype ideas and sometimes to debug into your webapp. (Sometimes it feels like hotswap when using the JVM :))I'll give a shoutout to a similar project I did with Python (but that doesn't auto-import modules) https://github.com/samzhang111/pit
nip 'function(l) { return /^var/.test(l); }'
is supposed to be better than egrep '^var'
?nip 'return /^var/.test(line)'
An implicit return, added if the input doesn't match /[function|return]/ would be nice, along with aliasing line as l, then you'd have:
nip '/^var/.test(l)'
This is precisely the problem with languages that make things easier for novices, once you are beyond that stage you will see yourself doing a lot of work to accomplish simple tasks. This is the reason I switched to Perl, from Python. Python was good for a month, once I started running into more complicated tasks, Perl starting emerging as the clear winner.
I could see all the 'end's being kind of a pain. Perl's ugly terseness is arguably an advantage for that use case.
Javascript is not the be-all-end-all programming language. Neither is Ruby, Awk, Haskell, or Prolog. However knowing more than one will help you grow in your chosen profession.
I will shed a single tear for grep though.
I'm going to seriously consider switching to Ag from ack, especially since my work source tree has recently switched to an SSD.