[LINUX] You should not use the --color = always option of the grep command

TL;DR When coloring grep, use **--color = auto instead of --color = always **.

environment

$ zsh --version
> zsh --version
> zsh 5.7.1 (x86_64-apple-darwin17.7.0)

Background and problems

In the process of proceeding with the task, processing such as extracting the line where col1 or col2 is a specific character string from the CSV file with the following structure and sorting in descending order of the value of col3. had.

test.csv


col1,col2,col3
a,a,10
a,a_1,20
a,a_2,5
a,a_3,10
a_1,a,25
a_1,a_1,30
a_1,a_2,20
a_1,a_3,10

As an example, let's say you need to extract a row where the value of col1 or col2 is ʻa. I simply ran the following command, thinking that I should get the desired output by sorting grepwith ʻa,.

Terminal


$ grep 'a,' test.csv | sort -t ',' -k 3 -rn

output


a,a_1,20
a,a_3,10
a,a_2,5
a_1,a,25  #Not numerically sorted
a,a,10    #Same as above

that? Why are the 4th and 5th lines not sorted in descending order? When I tried the behavior of sort alone without using a pipe, it seems that descending sort with col3 can be executed without any problem. If so, is there a cause in grep? Dozens of minutes (it's hard) to make trial and error with Atari. When I redirected the output of the command in question to a file, I finally figured out the cause.

Cause

Below is the content of redirecting the output of the previous command to a text file.

a,a_1,20
a,a_3,10
a,a_2,5
a_1,a,25
a,a,10

At first glance, I was confused by "Nanikore?", But as a result of my research, I found that this special character was ** ANSI escape code ** for coloring. Since I used to set ʻexport GREP_OPTIONS ="-color = always "in.zshrc, special characters for coloring were inserted in the part that matched the search pattern of grep. The problem this time is that it caused col3` on the 4th and 5th lines to become an uninterpretable string as a numerical value, and the descending numerical sorting did not work well. When I found out that the coloring setting was the cause, I commented out the above line and executed it again, and it worked as expected.

--Color option of grep

"But it's easier to see if you can color it, and I wonder if there is something", and when I looked up the --color of grep, I could specify either never, ʻauto, ʻalways. , I found that each has the following specifications.

--never: Do not color --ʻAuto: ** Color only when standard output is connected to the terminal ** Color (do not color when pipes or redirects) --ʻAlways: Always coloring (** changes the character string, which affects the subsequent processing **)

Looking at it like this, it seems that the merit of setting ** --color = always is basically thin **. ʻAutois probably sufficient for specifying aliases and environment variables for better visibility. If there is an exception, is it a situation where when the result ofgrep becomes long, it is passed to lessand viewed? The--color option of grepis used in preference to that of the environment variable at runtime, so if you pipe it toless as grep --color = always` only then I think I can achieve my purpose.

Articles that I used as a reference

Color the grep command Different results in grep results when using --color=always option

Lesson

The root cause of the problem was that when I started writing the program, I built a terminal environment with the knowledge I had heard, saying, "It seems that zsh has higher functionality than bash for the time being" and "It seems convenient to use this setting". It was there. It's quite natural, but when using the code and settings of the pioneer, it is important to correctly recognize the purpose and range of influence ... However, I think that it is only if you have some systematic knowledge that you can maintain that mindset, so I would like to continue to devote myself to it.

It was a valuable opportunity for self-discipline in a sense that I was troubled over time by the .zshrc that I casually copied and pasted in the past. I hope this little addiction case helps someone.

Recommended Posts

You should not use the --color = always option of the grep command
How to use the Linux grep command
How to use the grep command and frequent samples
What you should not do in the process of time series data analysis (including reflection)
Command collection (server edition) that you should know by isolating the cause of failure response
Grep so that grep does not appear at the time of grep
Explaining the mechanism of Linux that you do not know unexpectedly
Why you should use Pandas apply ()
Technical English> you use the boolean operators [and, or, and not] to ...> Boolean Operations — and, or, not
Why you should use urlopen instead of urlretrieve to download Python files
[Linux] Why do I, an infrastructure engineer, not use the "hostname" command?