Grep: a better way to temporarily disable colors?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep: a better way to temporarily disable colors?
# 1  
Old 03-06-2016
Grep: a better way to temporarily disable colors?

i was just using gnu grep

grep (GNU grep) 2.20
Quote:
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
and when running a command i kept getting really weird results, namely that eval and echo and various combinations would not run the command

Code:
BIN="${FFMPEG} -i ${INPUT} ${setX265} ${videoCrop} ${audioOptions} ${OUTPUT}.mkv"

with this "bad" result

Code:
crop=304:208:6:8 

and when i dumped the result to a file, it actually had this

Code:
^[[m^[[K^[[01;31m^[[Kcrop=304:208:6:8^[[m^[[K^[[m^[[K

finally it came down to my .bashrc file where i had defined

Code:
export GREP_OPTIONS='-irs --color=always --line-number --exclude-dir=.git'

in my script file i used this to temporarily disable grep colors, etc
Code:
unset GREP_OPTIONS

so it there a better way to turn off the grep options than having to switch this on/off?
# 2  
Old 03-07-2016
You can define shell variables temporarily for just the next command:
Code:
GREP_OPTIONS= command

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-07-2016
Maybe you should ask a different question. Why are you defining options to be applied to every invocation of grep you make in your .bashrc file if you don't want those options to be applied every time you invoke grep?

Have you considered defining an alias for grep that includes the options you want to apply only when grep is invoked interactively?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-07-2016
Quote:
Originally Posted by Don Cragun
Maybe you should ask a different question. Why are you defining options to be applied to every invocation of grep you make in your .bashrc file if you don't want those options to be applied every time you invoke grep?

Have you considered defining an alias for grep that includes the options you want to apply only when grep is invoked interactively?
Because when i grep from the command line i don't want to have to type in this every time.

Code:
'-irs --color=always --line-number --exclude-dir=.git'

if i did an alias, it could easily be stomped on by another shell or script running or if i used a different grep like BSD, it would probably choke on an alias.

i'm really looking for something simple that would just strip out the '^[K' formatting characters like with
Code:
 echo

or
Code:
printf

something that recognizes these as not to print or is so dumb that it ignores anything but the displayed string without attributes so that when i pipe something to my script file i don't have turn off or unset switches
# 5  
Old 03-08-2016
You can't have it both ways. If you want to make EVERY invocation of grep use the flags you are setting in GREP_OPTIONS, you are all set. But, as you have seen, that breaks scripts that depend on grep to do what the standards say grep is supposed to do.

If you want interactive invocations of grep to use a specific set of options, use an alias. If you want interactive invocations of grep to use a specific set of options if, and only if, you are running on a Linux system, define an alias for grep in your .bashrc if, and only if, uname indicates that you're starting bash on a Linux system.

Otherwise, I think you have two options:
1. rewrite every shell script you use (provided by you or anyone else) that uses grep,

OR

2. get rid of the definition of GREP_OPTIONS in your .bashrc and add a new utility (mygrep) in your bin directory ($HOME/bin/mygrep) that contains something like:
Code:
#!/bin/bash
grep -irs --color=always --line-number --exclude-dir=.git "$@"

that you install on Linux systems and on non-Linux systems install a symlink $HOME/bin/mygrep that points to wherever grep is located on that system and learn to use mygrep instead of grep whenever you use it interactively.
# 6  
Old 03-08-2016
Looks like a bug in grep.
It should never add terminal color sequences if output is directed to a file or to a pipe.
# 7  
Old 03-08-2016
Quote:
Originally Posted by MadeInGermany
Looks like a bug in grep.
It should never add terminal color sequences if output is directed to a file or to a pipe.
Not even when the option-argument is always (as in --color=always)? I don't have a GNU grep to test but I haven't seen where the GNU man page states any exceptions to always in the above case. (Maybe the "on the terminal" in that section of the description means that --color is ignored when output isn't directed to a terminal device, but it doesn't clearly state that behavior.)
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Red Hat

SSL/TLS renegotiation DoS -how to disable? Is it advisable to disable?

Hi all Expertise, I have following issue to solve, SSL / TLS Renegotiation DoS (low) 222.225.12.13 Ease of Exploitation Moderate Port 443/tcp Family Miscellaneous Following is the problem description:------------------ Description The remote service encrypts traffic using TLS / SSL and... (2 Replies)
Discussion started by: manalisharmabe
2 Replies

2. Solaris

Increase /tmp size temporarily

Hi all system Solaris 10 10/09 s10x_u8wos_08a X86 ufs file system I would like to install SunStudio. After the gui comes up it shows that i need to add swap space of at least 900m. the command swap-s shows 880m free. My question is can you temporarily increase swap by 1 or 2... (3 Replies)
Discussion started by: kc2dws
3 Replies

3. Shell Programming and Scripting

How to disable Enable/Disable Tab Key

Hi All, I have bash script, so what is sintax script in bash for Enable and Disable Tab Key. Thanks for your help.:( Thanks, Rico (1 Reply)
Discussion started by: carnegiex
1 Replies

4. Shell Programming and Scripting

temporarily suspend crontab

Issues: Cron jobs are running everyday at 8PM to hot backup database, archivelog, and some other files. Sometimes crontab scheduled backups need to be suspended for some other tasks randomly but doesn't happen all the time. Objective: I need to create simple script or command ( example :... (7 Replies)
Discussion started by: Paul.S
7 Replies

5. UNIX for Dummies Questions & Answers

Temporarily change the date / time

Hi - I'd like to know if it's possible to change the time on a Unix box temporarily, do some stuff, and then set it back to the correct time. I'm testing some code that reads a time (in hours) from a config file, and does something at that time. I want to be able to run regression test... (5 Replies)
Discussion started by: samgdavies
5 Replies

6. Shell Programming and Scripting

Can is set permissions temporarily?

I want to do just what the title says. Here's why: I am adapting a script that backups to a mount. In the script it has a catch where if the drive isn't RW then it unmounts and attempts to remount RW. # attempt to remount the RW mount point as RW; else abort $MOUNT -o remount,rw... (3 Replies)
Discussion started by: Movomito
3 Replies

7. AIX

how to block a user temporarily?

hi to all i'm a newbie on aix. how would i block temporarily a few users in accessing our aix server so that other users may not be affected by th cpu usage...because if all of them accessing our server the CPU utilization goes high... thanks winky (3 Replies)
Discussion started by: winky
3 Replies

8. HP-UX

Temporarily Disable User Account

I want to temporarily disable a user account on HP-UX at the start of a script and renable the account at the end of the script. What would be the best method on HP-UX to temporarily disable a user account? I know of the passwd -l option that will lock the account, but I do not see a flag for... (4 Replies)
Discussion started by: scotbuff
4 Replies
Login or Register to Ask a Question