I'm using Ubuntu 20.04.4 with stock grep version 3.4.
I find something I cannot explain, please help.
[/dev/pts/0(xterm-256color) 2023-04-04 10:28:01 ERR:0]
[chj @Ub20cppdev ~/test]
$ cat input3l.csv
"C:\Program Files (x86)\Comodo\",223743576,223920128
"C:\Program Files (x86)\Comodo\Dragon\",223743576,223920128
"C:\Program Files (x86)\Comodo\Dragon\dragon_s.dll",127097864,127098880
[/dev/pts/0(xterm-256color) 2023-04-04 10:28:07 ERR:0]
[chj @Ub20cppdev ~/test]
$ cat input3l.csv | egrep '(".+\\"),'
"C:\Program Files (x86)\Comodo\",223743576,223920128
"C:\Program Files (x86)\Comodo\Dragon\",223743576,223920128
[/dev/pts/0(xterm-256color) 2023-04-04 10:28:19 ERR:0]
[chj @Ub20cppdev ~/test]
$ cat input3l.csv | egrep '(".+\\"),.+'
[/dev/pts/0(xterm-256color) 2023-04-04 10:28:33 ERR:0]
[chj @Ub20cppdev ~/test]
$ cat input3l.csv | egrep '(".+\\"),.+' > egrep1.txt
[/dev/pts/0(xterm-256color) 2023-04-04 10:28:43 ERR:0]
[chj @Ub20cppdev ~/test]
$ cat egrep1.txt
"C:\Program Files (x86)\Comodo\",223743576,223920128
"C:\Program Files (x86)\Comodo\Dragon\",223743576,223920128
What you're seeing is almost certainly because of the interaction between color codes in the grep output and Windows-style (CRLF) line-endings in your input. You don't see the same when you redirect output, because the default egrep alias
only colors the output when writing to a terminal. Compare for example:
versus
You can see that the trailing
.*
(or in your case, withegrep
/grep -E
, trailing.+
) is capturing the carriage return character\r
and emitting it before the closing ANSI color sequence\033[m\033[K
, causing it to overtype the matched textfoo
.See also When piping grep after a curl request, regex works very strange