You are not logged in.
Pages: 1
Hi.
Can anyone explain why below outputs words longer than 6 characters by inserting the letter m into words such as zigzmamg (zigzag) and zodimamc (zodiac)? If appears to place an m before and after the character used as the argument for the first grep. Tried on Debian but the output there is as would be expected (as it is on Arch using Z shell). Also, it only seems to happen when the letter m is used as the argument for the second grep. Is this a known bug?
awk 'length($0) == 6' /usr/share/dict/words | grep a | grep m
Thanks.
Last edited by craig_mc (2025-10-14 12:55:37)
Offline
I don't encounter this issue:
[neumann@PDLap275W11 ~]$ cat words
zigzag
zodiac
[neumann@PDLap275W11 ~]$ awk 'length($0) == 6' words | grep a | grep m
[neumann@PDLap275W11 ~]$ bash --version
GNU bash, Version 5.3.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2025 Free Software Foundation, Inc.
Lizenz GPLv3+: GNU GPL Version 3 oder jünger <http://gnu.org/licenses/gpl.html>
Dies ist freie Software. Sie darf verändert und weitergegeben werden.
Es wird keinerlei Garantie gewährt, soweit es das Gesetz zulässt.
[neumann@PDLap275W11 ~]$
And where did you get /usr/share/dict/words from?
Sanity check
$ type grep
$ which grep
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
Also
shopt
though even for lastpipe that's wild - more likely some grep alias…
Offline
Thanks all.
It was caused by this.
alias grep='grep --color=always'
I can get back to cheating at word games now.
Offline
"--color=auto", do you understand what happened?
Edit: tbh, I can - but had not anticipated this happening.
Last edited by seth (2025-10-14 13:18:48)
Offline
Ah yes. grepping the ANSI color codes when you force grep to also throw them into the pipe, not just into an interactive TTY.
Edit: This is also fun:
[neumann@PDLap275W11 ~]$ alias grep='grep --color=always'
[neumann@PDLap275W11 ~]$ awk 'length($0) == 6' words | grep a | /bin/grep "\["
zigzag
zodiac
[neumann@PDLap275W11 ~]$
zigzag
zodiac
Last edited by schard (2025-10-14 13:25:10)
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
Meh, I wanted to the OP to think about that a bit
Offline
Meh, I wanted to the OP to think about that a bit sad
What is happening under the hood ? I mean I can't see any /n to notice some end of new line, it does seems that also /m is not a thing but maybe [m it does for ansi code. But not sure how it is linked to how grep works with string manipulations under the hood :C
str( @soyg ) == str( @potplant ) btw!
Offline
What is happening under the hood ? I mean I can't see any /n to notice some end of new line, it does seems that also /m is not a thing but maybe [m it does for ansi code. But not sure how it is linked to how grep works with string manipulations under the hood :C
This is indeed an ANSI terminal code issue. grep highlights the match in red using some ANSI commands, including "\e[01;31m" and "\e[m":
$ echo $'abc\ndef' | grep --color=always c | od -t a
0000000 a b esc [ 0 1 ; 3 1 m esc [ K c esc [
0000020 m esc [ K nl
0000025
This process is then repeated with the 'm' in those commands being highlighted:
$ echo $'abc\ndef' | grep --color=always c | grep --color=always m | od -t a
0000000 a b esc [ 0 1 ; 3 1 esc [ 0 1 ; 3 1
0000020 m esc [ K m esc [ m esc [ K esc [ K c esc
0000040 [ esc [ 0 1 ; 3 1 m esc [ K m esc [ m
0000060 esc [ K esc [ K nl
0000067
You can get similar results grepping for K (at least on my system), as well as some of the other characters that appear. If you keep grepping over and over again the output grows exponentially.
The moral of the story is that stdout is not always a terminal.
Last edited by 256 (Yesterday 05:13:57)
"Don't comment bad code - rewrite it." - The Elements of Programming Style (1978), Brian W. Kernighan & P. J. Plauger, p. 144.
Online
Pages: 1