Friday, June 29, 2018

I Thought That I Knew Regular Expressions

sed -e"s/\.\.//g" should remove .. from all stdin lines, right?  I am using Cygwin sed.

I do know regular expressions.  Font size in Terminal Window too small.  Those were commas it was failing to remove.

9 comments:

  1. The -e on the sed is wrong. You use that for specifying a file containing your regexps. You want something like:

    sed 's,\.\.,,g'

    E.g.

    $ cat foo
    There is one dot . then there are two dots .. and then there are three ... before we end.
    $ cat foo | sed 's,\.\.,,g'
    There is one dot . then there are two dots and then there are three . before we end.

    ReplyDelete
  2. Assuming the original puts a space between -e and the starting double quote, you're right, both per my memory and testing just now with Ubuntu 14.04, Gnu sed 4.2.2.

    But on Windows, maybe it's doing something wonky with the backslashes? What does echo "\.\." return?

    ReplyDelete
  3. It's been years since I used awk/grep/sed for work... even then it was under Solaris... I agree with you that the searched for pattern will be replaced with NULL, but are you sure the pattern you are looking for is ".." or "/.." ?

    ReplyDelete
  4. $ more test.txt
    .. This
    ...that
    no
    the other...

    $ cat test.txt | sed -e"s/\.\.//g"
    This
    .that
    no
    $ sed --version
    sed (GNU sed) 4.4
    Packaged by Cygwin (4.4-1)


    ReplyDelete
  5. See if this helps. I did a search for Kernighan's rule for backslashes. Depends on the number of layers of interpretation.

    http://cc2.savs.hcc.edu.tw/~chuavv/awk/Gory-Details.html

    ReplyDelete
  6. It depends on how the shell is handling quoted back-slashes. You might need two of them before each dot instead of one. I never can remember - I just try until it works.

    ReplyDelete
  7. PhaseMargin -f specifies the file. Without -e "unknown option to 's'"

    ReplyDelete
  8. Billy: Copied and pasted and it worked as you found. And with my own test file. I am thinking there is something between those periods.

    ReplyDelete
  9. My eyes are not what they once were. Those were commas, not periods.

    ReplyDelete