Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #5029
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!not-for-mail |
|---|---|
| From | Kaz Kylheku <kaz@kylheku.com> |
| Newsgroups | comp.programming |
| Subject | Re: Suggest a command line syntax |
| Date | Mon, 22 Dec 2014 20:09:35 +0000 (UTC) |
| Organization | Aioe.org NNTP Server |
| Lines | 70 |
| Message-ID | <20141222114514.682@kylheku.com> (permalink) |
| References | <m79mpq$p0h$1@speranza.aioe.org> |
| NNTP-Posting-Host | X+c6YNb3AaWMPA3YfA4opg.user.speranza.aioe.org |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | slrn/pre1.0.0-18 (Linux) |
| X-Notice | Filtered by postfilter v. 0.8.2 |
| Xref | csiph.com comp.programming:5029 |
Show key headers only | View raw
On 2014-12-22, Victor Porton <porton@narod.ru> wrote: > I am writing a program (in fact a Perl script), which should do replacement > of strings in a file, like this: > > AB->BC > BC->AB > YY->ZZ > > Note that Sed and relatives fail to do this task: It would replace BC back > with AB after replacing AB with BC, and this is not what I need. > > What command line syntax to specify strings to be replaced and replacement > strings for my command? This has nothing to do with syntax; it is a matter of semantics. The sed -e option syntax could have the semantics of parallelism: sed --fanatasy-parallel-behavior -e 's/AB/BC/g' -e 's/BC/AB/g' How that would work is that the left hand sides of all the regex replacements would be assembled into a big regex: (AB|BC) Based on which one matches, the appropriate replacement is applied. > I need an advice about the syntax of command line. If the behavior is strictly serial or strictly parallel, then there is no special feature needed in the syntax. Only if you want the expressivity of both. For instance, square brackets could group together replacements which are parallel. The open square bracket can combine with an option to create richer expressivity. Lack of square brackets indicates sequential processing. # -p[ denotes parallel substitutions $ ./super-replacer -p[ AB BC BC AB ] YY ZZ Here, AB is replaced with BC, in parallel as BC is replaced with AB. Then, YY is replaced with ZZ. A differently-optioned open bracket can do something else: # -s[ is a shorhand for swap $ ./super-replacer -s[ AB BC ] YY ZZ Here, -s[ AB BC ] expands to -p[ AB BC BC AB ]. -s[ can take an arbitrary number of pairs, all of which are swapped in parallel. Right rotation is handled with -r[, which takes any number of arguments to the close bracket. $ ./super-replacer -r[ AB BC CD ] The above instance is equivalent to -p[ AB BC BC CD CD AB ]. A right shift could be represented with -h[: $ ./super-replacer -h[ AB BC CD FOO ] This is equivalent to -p [ AB BC BC CD CD FOO ]. "AB" falls out of the "shift register", replaced by "BC", "BC" is replaced w ith "CD", and a "CD" is replaced with "FOO", which is effectively shifted in.
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Suggest a command line syntax Victor Porton <porton@narod.ru> - 2014-12-22 20:10 +0200
Re: Suggest a command line syntax Richard Heathfield <invalid@see.sig.invalid> - 2014-12-22 18:42 +0000
Re: Suggest a command line syntax Victor Porton <porton@narod.ru> - 2014-12-22 20:48 +0200
Re: Suggest a command line syntax Richard Heathfield <invalid@see.sig.invalid> - 2014-12-22 19:44 +0000
Re: Suggest a command line syntax Kaz Kylheku <kaz@kylheku.com> - 2014-12-22 20:09 +0000
Re: Suggest a command line syntax Robert Wessel <robertwessel2@yahoo.com> - 2014-12-22 14:46 -0600
Re: Suggest a command line syntax Kaz Kylheku <kaz@kylheku.com> - 2014-12-22 21:40 +0000
csiph-web