137 points · by akkartik · 14y ago · 106 comments
People praise perl, sed, awk, cut, etc. for being good at text processing. But the only reason they need these text-processing tools for pipelines is because they are trying to recover structure from the data that was already present before the previous stage of the pipeline threw that structure away by dumping it to flat text!
Text is obviously a convenient way for humans to view a program's output, so clearly it's useful that all Unix programs (ls, ps, etc) can dump their output as text. But there's no need to dump to text until the output is being sent to a human. If you're piping "ls | grep" there is no reason for "ls" to dump to text and "grep" to parse it back from text, especially since "grep" doesn't know anything about the format of ls's output. It would be way more convenient if you could say something like:
ls | grep 'file.size > 1M'
But the only way to do this today is to parse ls's output first. There would be no reason for this if ls could send structured data to grep.What I'm describing is similar to Monad, Microsoft's next-gen shell. AIUI it can send .NET objects between processes instead of flat text. But IMO it's too imposing to mandate a single object representation like .NET objects.
I'm experimenting with the idea of letting people specify the output of command-line utilities as a Protocol Buffer schema, for example:
message DirectoryEntry {
optional uint64 inode = 1;
optional string name = 2;
optional uint64 size = 3;
// etc.
}
I think this could be a compelling way of making the next generation of usability in command-line pipelines, by saving people from having to write ad-hoc parsers all the time.There are a variety of serialization schemes that are quite easy to parse and would be suitable for the output of most Unix command-line tools. BEncode from bittorrent would do nicely, and it's quite easy to parse. JSON would do nicely as well.
Better yet, unify the shell with a virtual machine that is used to implement the OS, and have everything available as 1st class Objects.
Yep, some friends of mine did this with JSON, but didn't make the schema explicit like I mean to: https://github.com/benbernard/RecordStream
> Better yet, unify the shell with a virtual machine that is used to implement the OS, and have everything available as 1st class Objects.
Please no. This is the Microsoft PowerShell approach, where everything is a .NET object. Once you start dictating representations of objects, you are dictating far too much about the implementation of individual pipeline nodes.
Until you need to incrementally process 2GB of data.
e.g.:
var sum = 0;
$("~").ls({"type": "file", "size": ">1M"}).each(function () {sum += $(this).size("mb");});
$.echo("total size of home directory: %d MB", sum); PS C:\Some Directory $sum = 0
PS C:\Some Directory dir | where { $_Length -gt 1MB } | %{ $sum += $_.Length }One of the things I feel Microsoft really got right.
I think the major thing missing from the world is easy piping a la Unix. You can't pipe your list of paying customer's email addresses to paypal at the command line with some switches - really, no matter how many switches you use - and bill each one the amount stated, because "paypal" is not a commandline app. You can't pipe the results of some long-running analysis to Twitter to announce that you've finished computing it, no matter how many switches you add, because twitter is not a commandline app.
The direction you're suggesting we take things is, in fact, a fuller API. These exist. They're slower and worse.
The amazing thing about text is that it's a lowest-common denominator. Think of communicating with a person.
Communication is faster with a mind-meld where you're looking at another person's face and picking up micro-expressions and body language. That's also the easiest thing to misinterpret.
When you pipe through such a 'human-readable' lowest common denominator, you're actually setting the ground for a very dynamic and versatile channel.
I don't know what the easy solution is to the problem you bring up, but I don't think your proposal is it.
Maybe there is no easy solution. Several unicode characters to abstract away the tab character and the newline character to instead n dimensions of characters, and corresponding negative characters so you can put something on a 'line' out of channel, (negative tab comment, tab, text, newline repeat) would probably solve some issues but is too abstract to even discuss. What we have really isn't that bad.
Nothing about my proposal has anything to do with an API. I'm just proposing a structured stream of data instead of an unstructured one.
> They're slower and worse.
I am proposing avoiding a serialize/parse step between every pair of pipeline elements (or using a more structured/optimized format if a serialization step is desired). Doing less work cannot possibly be slower.
> The amazing thing about text is that it's a lowest-common denominator. Think of communicating with a person.
Communicating with a person is an endless process of content negotiation. What are you and I talking about? Using our interface of plain text, we could be talking about literally anything. I could suddenly start talking about kumquat farming in Russia. At that point you could decide to follow suit and weigh in with your opinions on kumquat farming, or you could stop talking to me altogether because I've gone off-topic. If I start speaking complete jibberish, you could try to learn the language that I'm speaking, or you could start doing something equally nonsensical.
You and I can respond to unexpected communications in useful ways because we are fully autonomous, intelligent, sovereign beings that are capable of learning, creativity, and curiosity. I don't know about you, but I don't want my software to be autonomous or react to unexpected situations in unpredictable ways.
Data processing software should be as simple, predictable, and deterministic as possible. To use your example, if I somehow got an email address in my list called "send $1000 to Lucy," I don't want PayPal to decide to get smart and interpret the invalid email address as a command to send money to Lucy.
Right. So how does grep know it's talking to ls about dir contents with a (disk) size field and not talking to ps about process table contents with a (mem) size field? Some sort of slower content negotiation, I presume.
> cannot possibly
I suggest that every time you think that, you double-check your assumptions. In this case, I think you're just pushing all the extra hard work off onto some other part of the algorithm.
That's basically what an API is.
Apparently twitter and paypal ARE commandline programs!
The concept of lines is a human one. It is how humans parse. If humans could parse without needing the concept of a "line" then, e.g., there would be no problems with programming in C which has very shaky support for the concept of "lines".
But there are problems as we all know. It's proof that people do need to think in terms of "lines". Even though the computer does not need them. The only "problem" with this is that people are not computers.
I'm not sure anyone outside of the most unrealistic nerds would agree that this is a "problem".
1. As for the paper, I can think of at least one tool/language to work with text that does keep data in a binary format while one performs a series of transformations. It's not line-based. And I would guess that Mr. Pike does not know how to use it. It's fast and efficient. Probably faster than sam.
My problem is not with text per se, but with unstructured text. I'm fine with JSON in cases where efficiency is not a top concern.
Let me ask you this; how would you do the equivalent of this hypothetical command?
$ ls | structured-grep 'file.size > 1M'
The answer is that you can't in today's world without writing a parser (or some code that calls readdir/stat manually). That is the problem with unstructured text.Text formats like CSV that seem simple actually end up being hugely complicated once you push them to their limits. Nothing is worse than software that breaks once something unexpected happens, like a string that contains an embedded comma.
Though my boss did impress me by watching an OSI transaction in flight on our network monitor stop it and point to a dword and say thats wong and its Sprints broken x.400 implimentation.
I don't think Powershell really solves a problem, it's too complex to work with for the majority of problems. If i want complex data handling i write a script and put #!/usr/bin/env {bash,python,perl} in the first line.
I think the missing point here is that the nice, line-based, really simple approach is that this is how we speak and write and think. (in a series of flat words, so to speak). It's extremely easy to get into this kind of handling "data" and it's sufficient for a lot of tasks. I always admired what can be done with one line of bash/GNU utils.
As i said: If it get's more complex, we use a "real" programming language with more complex data structures anyway.
This is important for a lot of reasons not all of them are related to only development as a job.
(It hides the GUI and gives it a more ed-like interface that you can easily script, but this is kind of a hack for Plan 9-nuts ;)
Update: oh, no room in the title. To answer your question, it's from 1987. I found it via this morning's C vs Go article (http://news.ycombinator.com/item?id=4110480)
http://scholar.google.ca/scholar?hl=en&q=Structural+Regu...
Which yields the paper as the first result, showing its publishing year of 1987.
sam(and acme) sounds like an interesting method at first, but when you try it out, its really weird and slow to get stuff done. Its because for me, mouse is really inferior to keyboard to do the majority of the tasks.
This is an except from coders at work:
Seibel: Is there anything you would have done differently about learning to program? Do you have any regrets about the sort of path you took or do you wish you had done anything earlier?
Thompson: Oh, sure, sure. In high school I wish I’d taken typing. I suffer from poor typing yet today, but who knew. I didn’t plan anything or do anything. I have no discipline. I did what I wanted to do next, period, all the time. If I had some foresight or planning or something, there are things, like typing, I would have done when I had the chance. I would have taken some deeper math because certainly I’ve run across things where I have to get help for math. So yeah, there are little things like that. But if I went back and had to do it over I’m sure that I just wouldn’t have it in me to do anything differently. Basically I planned nothing and I just took the next step. And if I had to do it over again, I’d just have taken the next step again.
It would be interesting to know if Ken Thompson is using sam because he is a slow typist and hence doing stuff the sam way (mouse oriented) boosts his productivity or simply because he likes using sam.
(in what follows HN is mangling asterisks, so I used 'X')
Tokenizing a language is much simpler though. So I just did that, and let wildcards match like <token>X? up to the next expression boundary at the same nesting level - the boundaries were ',' ';' ')' '}' and ']' when I tried this on java, C and perl.
This turns out to be simple enough and powerful enough to be useful. For instance, your example:
boost::bind(& ${id} $( :: ${id} )+, $( boost::ref( Xthis ) )|ref| $.X )
Would be: boost::bind(& $id1$ :: $id2$, boost::ref( Xthis ) ) (for 2 args - I'd need to repeat for more args like so: boost::bind(& $id1$ :: $id2$, boost::ref( Xthis ), $id3$)
This works because $id1$ is non-greedy, and whitespace is ignored. I did tend to tweak the tool to what I was searching for (I'd have dropped ',' as a delimiter for this one), which I guess is cheating on keeping the syntax simple!
1. If it's longer than 15 lines, rewrite it in {python,ruby,perl} 2. Never accidentally rewrite grep, sed or awk.
What Pike describes is analogous to the RecordReader in Hadoop.
( RECORD_PATTERN=somepattern; my | pipe | line | whatever )
rather than
my -R somepattern | pipe -R somepattern | line -K somepattern | whatever -R somepattern
Separate tools have the advantage (over regex) of handling nesting properly, which could certainly be significant.
Okay, thanks, but I've known about basic automata theory since I was an undergrad, two decades ago.
I had something like this in mind:
ls -af | jsonify 'ls -af' | this_reads_a_json_stream
The jsonify command would retrieve and run a script from a central repository, which people could contribute code to. This way, the parsing efforts of one coder could be re-used by the rest of the world. foo(shortArg1, shortarg2, shortArg3);
is easy to find, but foo(longArg1,
methodCall().stuff(),
evenMoreComplicatedStuff);
is much more difficult to grep for.Thanks!
Occasionally you can just convert it to lines first as well and problem solved.
It's inelegant enough that I sometimes do a two-step process instead: 1) transform the input so that whatever view I want of it maps to a line-oriented format; and then 2) process the result in the standard Unix line-oriented fashion.
Something like (perl):
&visit_matches(
{ ' +' => sub { $x += length( $1); },
'#+' => sub { print $1, ' at ' $y, ',', $x'; $x += length( $1); },
'\n' => sub { $y++; }
});(not an exact match to the pseudo-awk, but enough to get the idea)
As for your second statement, if you would have read the paper you would have seen why it is not the case.
A bit late in the 80s, but still.
[Edit] I read your comment too quickly and hadn't noticed that you said "early 80s". My apologies.
When this was written companies were advertising "Cut and Paste" as a big feature for word processors.