0

I am trying to write shell script as below to search user inputted word and highlight the word in the search result -

# User input:
echo -n -e "What you wanna search:" 
read userinput

Save the search result in a file

egrep "$input" /var/log/auth.log > result.txt

Display the result column wise with highlighted user input value

column -t result.txt | grep '$userinput' --> (this is not working. It's not highlighted the word)

For example: I am looking for root user in the search result and I want to highlight the root word being highlighted but it's not working.

$ column -t result.txt | grep -n 'root' *--> This is working* 
$ column -t result.txt | grep -n '$userinput' **--> This is not working**

Any idea what's I am doing wrong?

phuclv
  • 30,396
  • 15
  • 136
  • 260

1 Answers1

1

Just add --color=always to your grep command. For instance:

column -t result.txt | grep --color=always '$userinput' 

When you call grep interactively, it uses color by default. But when you call it in a shell script, it doesn't use color, so you need to force it with --color=always

SparedWhisle
  • 4,393