I’ve came across a question transform this text with sed [sed only]
and it got me wondering what class of problems is solvable by stream editors or programs/computational models with similar constraints.
For E.g Regular expressions can solve problems that require fixed/const amount of memory (Can solve all problems that have a Deterministic Finite state automa)
My question is what are the computational limits/constraints of sed in particular and any stream editor in general and what class/classes of problems can be solved by it?
(after mapping it some text alphabet set if need be)
1
First of all, I would say that "can be done (or solvable)" != "best choice"
Let’s see some examples:
-
You mentioned regex. Regex can detect prime numbers. Would you use regex for it?
-
text processing with some math calculation, can be done by
sed
too. however, if awk is available, would you go withsed
? -
also something like, parsing a text with a certain pattern, when 3rd match, do A, 6th match, do B, xth match do Y. this can be done by sed as well by playing with hold space. but think about it how straightforward if you use awk to solve it.
-
think about writing a normal
for(i=1;i<100;i++)
loop with sed
gnu sed supports passing matched part to external programs to do further processing then get result back. (like system() or getline
in awk) So you could imagine what kind of problems sed can do if your sed is backup-ed with bc, sed, nl, seq, join, cut, sort, paste, grep even cp, mv, rm, find... and awk
The best choice for same problem could be different to different people. say a problem, sed is the best choice from your point of view. However he is not good at sed, but knows awk well, he thought awk is best choice. She knows python well.. and I am an expert of MS-Excel….
my 2cents.
2
I use sed primarily in a *nix environment where the problem to be solved is a straightforward data transformation problem. For example, adding or removing columns from .csv data or modifying an existing date in a field. Also, sed is warranted where writing another piece of software written in Python, Perl, Clojure, awk, or your favorite language does not support the overhead of another module’s maintenance.
I have not used sed’s advanced features, like swapping registers, and probably will not, because going beyond a simple data transformation would warrant the overhead of a more fully fledged program.
1