5 ms·
This is cool and useful, but not all unix programs follow this convention: * find * cal * vi * emacs * ls These don't use one of standard input/sta
by wglb 8y ago
This is cool and useful, but not all unix programs follow this convention:
* find
* cal
* vi
* emacs
* ls
These don't use one of standard input/standard output. (edited) and are not fully pipeable.
I don't recall seeing a list of programs--tools, in the original description--that distinguish between pipeable and not-pipeable programs.
Also, none of the corrective cat/grep code in these threads point out that grep in fact takes file names, so "cat foo | grep stuff" is just a silly no-op.
- fooblat 8y agoWell find, cal, and ls don't take input at all. And vi (well vim) in fact does. If you invoke it with a - for the file name argument, it will read standard input into the buffer. I can't comment on emacs as I don't use it much.
- wglb 8y agoAh, correct on that point. What about standard output? That is, can vi be used in a pipe?
- rmilk 8y agoI use vim as a visual pipe or pipe debugger with undo when I need to perform a series of transformations (grep/sort/cut/lookup/map/run other data tool). Obviously geared towards text files because it is vim. The ! command sends the selection through external command(s) as STDIN and then replaces the selection with STDOUT from the command(s). For example, grep or sort, but can be any command that works with pipes. Buffer is replaced with output (sorted file for example). Undo with U to go back to original data. Redo with R to go forward to transformed data. Command line history is available to add more commmands or correct when you type ! again. Edit a file. Select block (Visual mode shift-V) and type ! or use the whole file with gg!G command. Type in the commands you need to run. Vim also reads stdin if you give - as the filename, like “ls -l | vim -“ so you we can use it at the end of a pipe instead of redirecting to a file. Like I said, I use it as an interactive debugger to assemble pipelines and see the results. vi / vim filter commands: http://vimdoc.sourceforge.net/htmldoc/change.html# http://vimdoc.sourceforge.net/htmldoc/change.html#!
- flukus 8y agoWith some redirection magic it can be made to, but I find that magic is always too much stuffing around to make it useful. Inside scripts though I like to pipe to vim and have it output to a file: pipeline | vim - +"file $mytempfile" Then if you quit vim without saving $mytempfile won't exist, if you save and quit it will and further processing can be done. I've got a view scripts like this to to things like viewing csv files after being piped through column, after saving they're converted back to csv.
- cjbprime 8y agoIt's true that `ls` and `find` don't take stdin, but their stdout is often piped: consider `ls | wc -l` or even something like (more complicated than) `find . -name \*.txt | xargs vi`.
- anthk 8y ago- Find has some switches to sanityze the output - Cal can be parsed (it's shows in the Unix Programming Environment, from 1983) - Vi is a visual editor, it can be used as a front-end for ed/ex commands for I/O anyway. Kinda like the acme(1) of its day. You have both :w and :r. And, hint: it can input text from external pipes. - Emacs is not Unix - ls(1) is not meant to be parsed on files' content, that's the shell globbing for.
- wahern 8y agoFWIW, POSIX specifies the ls output format for -l and several other flags. For example, If the -l option is specified, the following information shall be written for files other than character special and block special files: "%s %u %s %s %u %s %s\n", <file mode>, <number of links>, <owner name>, <group name>, <size>, <date and time>, <pathname> There's no stat command in POSIX. More practically, the BSD and GNU versions of stat are completely incompatible. If you want to query a file's size or other metadata from a portable shell script, you need to parse the output of ls.