1

is this a bug in cygwin GNU sed 4.4 when run from a cmd window?

this works

C:\>echo asdf| sed "s/a/w/"
wsdf

C:\>

this works (apart from that the output isn't quite right in that it shouldn't have a space before the 'den')

C:\>echo  VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
 den VID_abc p

C:\>

these don't work at all. the output should have no space at the start and should say den...

C:\>echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
 pn VID_abc_def

C:\>echo  VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
 pen VID_abc_def

C:\>echo   VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
 pden VID_abc_def

C:\>echo    VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
 p den VID_abc_def

C:\>

this works

C:\>echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
den VID_abc p

C:\>

as soon as I add the \2 then it doesn't work and no longer starts with 'den'

C:\>echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
 pn VID_abc_def

C:\>

Yet from a cygwin window it works fine

$ echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
den VID_abc_def p

I'm not sure that there is a sed for windows. Gnuwin32 stuff is all old. Cygwin sed is more up to date but doesn't seem to work from cmd.. I may stick with perl Perl for matching with regular expressions in Terminal? and https://stackoverflow.com/questions/4794145/perl-one-liner-like-grep

C:\>echo VID_abc_def|perl -0777 -pe "s/(VID.*)(_def.*)/den \1\2 p/"
den VID_abc_def p

C:\>

There doesn't seem to be a reliable up to date sed for windows at the moment and hasn't been for years.

$ sed --version
sed (GNU sed) 4.4
Packaged by Cygwin (4.4-1)
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: </...>.

and

C:\>sed --version
sed (GNU sed) 4.4
Packaged by Cygwin (4.4-1)
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <...>.

C:\>
0

2 Answers 2

3

the issue is with "echo" as you are not using the cygwin one.

$ cmd
Microsoft Windows [Version 10.0.18363.720]
(c) 2019 Microsoft Corporation. Alle Rechte vorbehalten.

D:\cygwin64\home\Marco>echo  VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
 den VID_abc p

D:\cygwin64\home\Marco>echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
den VID_abc p

with cygwin echo

$ echo  VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
den VID_abc p

$ echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1 p/"
den VID_abc p

added by barlop

You're right..

C:\>echo abc|xxd -p

6162630d0a (xxd available from vim7 or cygwin.. 0d=\r)

(windows echo output adjusted to make it like cygwin echo, in line below)

C:\>echo VID_abc_def| tr -d '\r'|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
den VID_abc_def p

(Note that windows echo can have trailing space(s) if you do echo abc | or echo abc | and to avoid that you'd do echo abc|(no space left of the pipe), whereas cygwins wouldn't even if you had echo abc |.

(cygwin echo, in line below)

C:\>c:\cygwin\bin\echo VID_abc_def|sed -r "s/(VID.*)(_def.*)/den \1\2 p/"
den VID_abc_def p

So a \r is coming from windows echo. and is tripping up cygwin sed.

0

An answer? Maybe, maybe not:

One step of analysis to add for sure:

Add
... | tr -d "\r" "_" or even
... | od -t x1z to your commands;

od shows the actual characters that are printed.

tr as shown replaces any CR characters (plausibly added by cmd's echo),

... which might be the culprit.


END ADVICE #1: Don't mix cygwin and cmd -software.
Also; I believe these are issues with Windows' CR+LF line endings, in free software compiled for Windows.

1
  • That'd be tr -d '\r' you missed the -d No big issue mixing, just awareness of that \r issue with, as the other answer said, cmd echo.
    – barlop
    Mar 15, 2020 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .