6 ms·
Please show me how to do `ls | grep -i old` in Python? For a short while I was following https://github.com/matz/streem https://github.com/matz/streem (from th
by nixpulvis 5y ago
Please show me how to do `ls | grep -i old` in Python?
For a short while I was following https://github.com/matz/streem https://github.com/matz/streem (from the original author of Ruby) with interest in possibly being adapted to superseed Bash. I have no idea how realistic that actually is.
I also use fish a lot, but it has a lot of it's own problems.
The thing about all these "shells" that really seperates them from Python (et all) is that they lookup commands from PATH and execute them directly. Anything else is simply not a shell language no matter how great it is at scripting.
- tkot 5y agoHow about Groovy? ("ls".execute() | "grep -i old".execute()).waitForProcessOutput(System.out, System.out) It can also be written as "ls".execute().or("grep -i old".execute()).waitForProcessOutput(System.out, System.out) It's not exactly the same because it uses a thread and a buffer instead of an OS pipe but I guess it's close enough.
- josephcsible 5y agoI wonder why Groovy decided to map "or" to the shell's "|". Wouldn't it have made more sense to map "or" to the shell's "||" and "pipe" to the shell's "|"?
- tkot 5y agoI think this is just how operator overloading works in Groovy: https://groovy-lang.org/operators.html#Operator-Overloading https://groovy-lang.org/operators.html#Operator-Overloading "Or" is the name of the "|" operator. From the looks of it it's impossible to overload "||" in groovy. By the way the "|" operator and the "or" method both call "pipeTo" directly so a third way to write it would be: "ls".execute().pipeTo("grep -i old".execute()).waitForProcessOutput(System.out, System.out)
- nixpulvis 5y agoWhy should I need to explicitly state that I want to wait for my process output? I'm in a shell, I put a `&` afterwards if I want things in the background. The model is synchronous by default.
- jcranmer 5y ago> Please show me how to do `ls | grep -i old` in Python? for filename in os.listdir(os.curdir): if 'old' in filename.lower(): print(filename) One of the advantages of using a language like Python is you don't need to set up a lengthy pipeline of program executions just to reformat program output. In my personal experience using Python for basic automation scripting, I just haven't really come across any need for me to pipe output from one command into another. The problem I have with a lot of shells is that their variable syntax is almost completely broken if you want to do anything slightly harder than trivial. A recent example I had was trying to write a script that reads a shell and executes the last line with some extra arguments. So that should be `PROG=$(tail -n1 commands.sh); creduce-script "$PROG"`, nothing hard about that... except the last line was `clang "-DFOO= " bar.c`.
- nixpulvis 5y agoI'm not arguing that something like Python is nice for when I'm writing scripts out and saving/committing them. A shell is a REPL designed to use quickly and easily for one-off jobs. Shell scripts are also sometimes just simpler to string together than referencing a whole new language.
- justsomehnguy 5y agols | ? name -eq old ls | ? Name -match '^old$' Any other /property/ of the file (datetime fileds, name, extension, path) can be matched woth a string representation or an object methods. But yes, this isn't Python.
- eternityforest 5y agoYou probably wouldn't do that in Python because Python isn't a shell. It was never designed to write mini programs in 5 seconds as a way to interact with the computer. You would write a script with os.listdir and 'if "old" in path' if you wanted to automate it. If you were doing things interactively in a python minded OS, you might either have something much like shell is today(Maybe very stripped down without a focus on programming features beyond glob, expansion, and pipes), or you would have python library of misc utils. Maybe you would do x=ls() search(x, "old") But that would be rather slow. Maybe you'd have some completely new model of text based interaction that was more menu driven and looked a lot like VS Code's command palette. You'd press tab at the beginning of a line(Arbitrary easy to reach shortcut), and get your command pallet. Start typing and fuzzy search happens. Select "Search filenames", and maybe that's a common enough snippet that it would be built in. Now you have that whole script, and your cursor is already in the search term field. Press tab there. You get a menu of recently set variables, recently used short strings, etc, that the heuristic might want you to have. With better UI we could afford a more explicit traditional language syntax. Both scripting and shell could be a lot better.