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:

PhaseMargin said...

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.

hga said...

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?

jnvm34 said...

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 "/.." ?

Billy Oblivion said...

$ 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)


JLW III said...

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

StormCchaser said...

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.

Clayton Cramer said...

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

Clayton Cramer said...

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.

Clayton Cramer said...

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