grep, use **
--color = auto instead of
--color = always **.
$ zsh --version > zsh --version > zsh 5.7.1 (x86_64-apple-darwin17.7.0)
In the process of proceeding with the task, processing such as extracting the line where
col2 is a specific character string from the CSV file with the following structure and sorting in descending order of the value of
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
col2 is ʻa
. I simply ran the following command, thinking that I should get the desired output by sort
$ grep 'a,' test.csv | sort -t ',' -k 3 -rn
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.
Below is the content of redirecting the output of the previous command to a text file.
[01;31m[Ka,[m[Ka_1,20 [01;31m[Ka,[m[Ka_3,10 [01;31m[Ka,[m[Ka_2,5 a_1,[01;31m[Ka,[m[K25 [01;31m[Ka,[m[K[01;31m[Ka,[m[K10
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 "
, 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.
"But it's easier to see if you can color it, and I wonder if there is something", and when I looked up the
grep, I could specify either
, ʻalways. , I found that each has the following specifications.
never: Do not color
: ** 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 **.
is 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 less
and viewed? The--color
option of grep
is 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.
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.