Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #5025 > unrolled thread

Suggest a command line syntax

Started byVictor Porton <porton@narod.ru>
First post2014-12-22 20:10 +0200
Last post2014-12-22 21:40 +0000
Articles 7 — 4 participants

Back to article view | Back to comp.programming


Contents

  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

#5025 — Suggest a command line syntax

FromVictor Porton <porton@narod.ru>
Date2014-12-22 20:10 +0200
SubjectSuggest a command line syntax
Message-ID<m79mpq$p0h$1@speranza.aioe.org>
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?

I need an advice about the syntax of command line.

-- 
Victor Porton - http://portonvictor.org

[toc] | [next] | [standalone]


#5026

FromRichard Heathfield <invalid@see.sig.invalid>
Date2014-12-22 18:42 +0000
Message-ID<VCZlw.288824$td6.82371@fx30.am4>
In reply to#5025
Victor Porton 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?
> 
> I need an advice about the syntax of command line.

The idea sounds simple enough (although I can see some difficulties in 
writing the code in a robust and predictable way), but I want to be sure 
that I understand you. If I have it right, the user identifies a number of 
pairs of strings - to take your example, the pairs would be { AB, BC }, { 
BC, AB }, and { YY, ZZ } - and the program then does the following:

1) for each pair:
     identify all instances of the left member, and note its start and end 
position.

This loop is done *first*.

2) for each pair:
    for each instance:
      replace the left member with the right member.

So, for example, if we have the string

ABNNABNNBCNNZZNNYY

and the pairs { AB, BC }, { BC, AB }, { YY, ZZ }
we proceed as follows:

Phase 1: split

AB
NN
AB
NN
BC
NNZZNN
YY

Phase 2: replace

BC
NN
BC
NN
AB
NNZZNN
ZZ

Phase 3 (possibly incorporated into Phase 2): splice

BCNNBCNNABNNZZNNZZ

The difficulty comes when you have overlapping search strings. You need to 
be very careful how you handle (and document) them.

Anyway, assuming that's the general idea, the command line syntax could be 
something like:



programname -<options> -pairs p1a p1b [p2a p2b [p3a p3b [...]]] filespec

Options would be, for example, case-insensitivity (-i) and the like. Then 
would come the pairs - the switch '-pairs' might be considered surplus to 
requirements, so feel free to ditch it - and finally there is the file 
specification (if you're on Unix, you can just let globbing happen).

Alternatively, if there are many pairs, you could put them in a tab-
separated file, one pair per line, and specify the filename - e.g.

programname -i -f=swappairs.tsv j*.txt

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#5027

FromVictor Porton <porton@narod.ru>
Date2014-12-22 20:48 +0200
Message-ID<m79p1a$vff$1@speranza.aioe.org>
In reply to#5026
Richard Heathfield wrote:

> Anyway, assuming that's the general idea, the command line syntax could be
> something like:
> 
> 
> 
> programname -<options> -pairs p1a p1b [p2a p2b [p3a p3b [...]]] filespec

This way it is easy to confuse <filespec> with pairs.

> Options would be, for example, case-insensitivity (-i) and the like. Then
> would come the pairs - the switch '-pairs' might be considered surplus to
> requirements, so feel free to ditch it - and finally there is the file
> specification (if you're on Unix, you can just let globbing happen).
> 
> Alternatively, if there are many pairs, you could put them in a tab-
> separated file, one pair per line, and specify the filename - e.g.
> 
> programname -i -f=swappairs.tsv j*.txt

Hm, maybe to use CSV instead? It allows encoding arbitrary strings including 
comma and tab.

-- 
Victor Porton - http://portonvictor.org

[toc] | [prev] | [next] | [standalone]


#5028

FromRichard Heathfield <invalid@see.sig.invalid>
Date2014-12-22 19:44 +0000
Message-ID<Fw_lw.243015$v42.203672@fx19.am4>
In reply to#5027
Victor Porton wrote:

> Richard Heathfield wrote:
> 
>> Anyway, assuming that's the general idea, the command line syntax could
>> be something like:
>> 
>> 
>> 
>> programname -<options> -pairs p1a p1b [p2a p2b [p3a p3b [...]]] filespec
> 
> This way it is easy to confuse <filespec> with pairs.

So you put a marker between them, e.g. -f filespec

> 
>> Options would be, for example, case-insensitivity (-i) and the like. Then
>> would come the pairs - the switch '-pairs' might be considered surplus to
>> requirements, so feel free to ditch it - and finally there is the file
>> specification (if you're on Unix, you can just let globbing happen).
>> 
>> Alternatively, if there are many pairs, you could put them in a tab-
>> separated file, one pair per line, and specify the filename - e.g.
>> 
>> programname -i -f=swappairs.tsv j*.txt
> 
> Hm, maybe to use CSV instead? It allows encoding arbitrary strings
> including comma and tab.

Sure, but I deliberately chose tab because you're more likely to want to 
include a comma in a pair than include a tab in a pair. But whatever floats 
your boat.

(If you follow all the advice in this post and my last, you'll end up with 
two -fs with different meanings! Choose a different switch for the tsv/csv 
file - e.g. -m for meta or -p for pairfile or something like that.)

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#5029

FromKaz Kylheku <kaz@kylheku.com>
Date2014-12-22 20:09 +0000
Message-ID<20141222114514.682@kylheku.com>
In reply to#5025
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.

[toc] | [prev] | [next] | [standalone]


#5030

FromRobert Wessel <robertwessel2@yahoo.com>
Date2014-12-22 14:46 -0600
Message-ID<pg0h9a51os2c13r0vlcbphrsj1qul14hac@4ax.com>
In reply to#5025
On Mon, 22 Dec 2014 20:10:06 +0200, 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.


Sure it will, you just need to do it a bit differently.  Do the
following:

  change AB -> QQQQ
  change BC -> AB
  change YY-> ZZ
  change QQQQ -> AB

If "QQQQ" conflicts with another string in your source, use a
different temporary string in the first and fourth steps.

[toc] | [prev] | [next] | [standalone]


#5031

FromKaz Kylheku <kaz@kylheku.com>
Date2014-12-22 21:40 +0000
Message-ID<20141222133135.43@kylheku.com>
In reply to#5030
On 2014-12-22, Robert Wessel <robertwessel2@yahoo.com> wrote:
> On Mon, 22 Dec 2014 20:10:06 +0200, 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.
>
>
> Sure it will, you just need to do it a bit differently.  Do the
> following:
>
>   change AB -> QQQQ
>   change BC -> AB
>   change YY-> ZZ
>   change QQQQ -> AB
>
> If "QQQQ" conflicts with another string in your source, use a
> different temporary string in the first and fourth steps.

This can be made "air tight" if it includes an extra pass which
detects whether the rename string like QQQQ occurs in the file.

    while not long enough (temp_string_list ) do
      do
         t = generate_unique_temp_string();
      while (t occurs in input file or t occurs in replacement strings)
      push t onto temp_string_list
    done

After this, you have enough required temp strings.

Note that you never want the temp strings to actually have a form like QQQQ.
This is because QQQQ has a suffix and a prefix which can combine to
create a spuriouis QQQQ.

If the original text contains no QQQQ, but contains QQAB, after an
AB -> QQQQ a substitution you have QQQQQQ, oops!

A brace-delimited token like {QQQQ} would be better.  An insertion of {QQQQ}
cannot combine with fragments of {QQQQ} to produce a spurious {QQQQ}.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web