3 ms·
I know the text stream interface has lots of limitations and formats are often unspecified or underspecified. I would just add that if you happen to have numbe
by schoen 5y ago
I know the text stream interface has lots of limitations and formats are often unspecified or underspecified.
I would just add that if you happen to have numbered fields with consistent delimiters (and no quoting or escaping of delimiters), you can do all of these things with cut, sort, grep, and head, to say nothing of awk, which can typically do them in a single line.
ls -l foo/ | awk '$NF == "bar.baz" {print $NF, $5}' | head -10
The numbered fields with consistent delimiters part is a big if, and of course the fields are numbered and not named, so you don't get to do the "name,size" thing (you have to know explicitly that for this particular command, the filename is the last field and the size is the fifth field).
One direction that some people have gone is to treat JSON as a universal form of structured data (where text is a universal form of unstructured data). You could rebuild many Unix tools or near-equivalents to work well with JSON records instead of, say, lines. Your pipe example would be pretty straightforward in jq if you had a version of ls that gave you JSON outputs.
Here's a crummy, primitive version of ls that gives JSON outputs. :-)
#!/usr/bin/env python
import json, os, sys
ret = {}
for arg in sys.argv[1:]:
here = {}
for entry in os.listdir(arg):
path = os.path.join(arg, entry)
stat = os.stat(path)
here[entry] = {
"size": stat.st_size,
"ctime": stat.st_ctime,
"atime": stat.st_atime,
"mtime": stat.st_mtime,
"uid": stat.st_uid,
"gid": stat.st_gid,
"mode": oct(stat.st_mode)[2:],
}
ret[arg] = here
print(json.dumps(ret))
With that, you could then use jq to do various kinds of selection and aggregation. (The specific data model here is probably not right yet, because it allows, and requires, exactly one level of subdirectory nesting in each listing.) I'm not sure how the jq learning curve (combined with JSON-refitted Unix tooling) would compare to something like PowerShell, which is super-verbose compared to Unix traditions.
Apparently there are several ongoing efforts to create Unix shells that use JSON or other structured data more natively, but I have yet to try any of them.
https://ilya-sher.org/2018/09/10/jq-is-a-symptom/ https://ilya-sher.org/2018/09/10/jq-is-a-symptom/
https://github.com/ngs-lang/ngs https://github.com/ngs-lang/ngs
(also see the numerous shell projects linked from the NGS GitHub page!)
- Someone 5y agoI know you can do it but the trouble with Unix is that its user interface is horrid (http://www.ceri.memphis.edu/people/smalley/ESCI7205F2009/misc_files/The_truth_about_Unix_cleaned.pdf http://www.ceri.memphis.edu/people/smalley/ESCI7205F2009/mis..., discussed in https://news.ycombinator.com/item?id=9558455 https://news.ycombinator.com/item?id=9558455) The list of inconveniences in the shell is just too long. It is death by a thousand cuts. I’ve seen scripts grabbing file sizes from the output of ls -l by using character offsets in each line, which, aside from the usability issue, means such scripts will break if files get too large to fit the space ls thinks is sufficient.