Sample Header Ad - 728x90

Why is my string changing in this pipeline?

0 votes
1 answer
120 views
I was trying to pipe an output url into the open command but found several instances where despite changing the color settings of grep or piping further output through other utils like awk or cut I was still getting special chars in the output. As shown below: enter image description here The first 2 times I run the open command I get sent two these two urls respectively: enter image description here enter image description here You can see the special chars being tacked onto the urls despite being piped through cut, or changing the color settings. This is confirmed with the od command: ➜ ~ apm show vim-mode | grep https | cut -d' ' -f2 | od -atx1 0000000 esc [ 4 m h t t p s : / / g i t h 1b 5b 34 6d 68 74 74 70 73 3a 2f 2f 67 69 74 68 0000020 u b . c o m / a t o m / v i m - 75 62 2e 63 6f 6d 2f 61 74 6f 6d 2f 76 69 6d 2d 0000040 m o d e esc [ 2 4 m nl 6d 6f 64 65 1b 5b 32 34 6d 0a ➜ ~ apm show vim-mode | grep --color=none https | cut -d' ' -f2 | od -atx1 0000000 esc [ 4 m h t t p s : / / g i t h 1b 5b 34 6d 68 74 74 70 73 3a 2f 2f 67 69 74 68 0000020 u b . c o m / a t o m / v i m - 75 62 2e 63 6f 6d 2f 61 74 6f 6d 2f 76 69 6d 2d 0000040 m o d e esc [ 2 4 m nl 6d 6f 64 65 1b 5b 32 34 6d 0a 0000052 The next two outputs try to use awk to strip off the url from apm however they also introduce special chars, and the open command then tries to open them as files. This is confirmed with od: ➜ ~ apm show vim-mode | awk -F' ' '/http/ {print $2}' | od -atx1 0000000 esc [ 4 m h t t p s : / / g i t h 1b 5b 34 6d 68 74 74 70 73 3a 2f 2f 67 69 74 68 0000020 u b . c o m / a t o m / v i m - 75 62 2e 63 6f 6d 2f 61 74 6f 6d 2f 76 69 6d 2d 0000040 m o d e esc [ 2 4 m nl 6d 6f 64 65 1b 5b 32 34 6d 0a The solution has been to use a lovely [sed oneliner](http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed) , sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" that succeeds at stripping off all the special chars. The final open command uses this oneliner and succeeds at going to https://github.com/atom/vim-mode as expected. This is confirmed by od: ➜ ~ apm show vim-mode | awk -F' ' '/http/ {print $2}' | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | od -atx1 0000000 h t t p s : / / g i t h u b . c 68 74 74 70 73 3a 2f 2f 67 69 74 68 75 62 2e 63 0000020 o m / a t o m / v i m - m o d e 6f 6d 2f 61 74 6f 6d 2f 76 69 6d 2d 6d 6f 64 65 0000040 nl 0a 0000041 So this leaves some questions: 1. Why isn't the grep --color=none working? 2. Why are my various other utils tacking on special chars? I suspect it has something to do with my shell settings. I'm using [zim](https://github.com/Eriner/zim) with the [gitster](https://github.com/shashankmehta/dotfiles/blob/master/thesetup/zsh/.oh-my-zsh/custom/themes/gitster.zsh-theme) theme **edit** setting TERM=dumb appears to have no affect: ➜ github-application master ✓ diff <(TERM=dumb apm show vim-mode | od -atx1) <(apm show vim-mode | od -atx1) ➜ github-application master ✓ setting NPM_CONFIG_COLOR=false also appears to have no affect: ➜ ~/w/s/g/m/todo master ✓ diff <(NPM_CONFIG_COLOR=false apm show vim-mode | od -atx1) <(apm show vim-mode | od -atx1) ➜ ~/w/s/g/m/todo master ✓ I think it's because of the underline in the url. I've been exploring it further in [another question](https://stackoverflow.com/questions/40336126/where-does-the-coffeescript-underline-method-come-from)
Asked by mbigras (3472 rep)
Oct 29, 2016, 05:07 AM
Last activity: Oct 31, 2016, 06:00 PM