1

Below is a file named sites.txt:

google is good
yahoo is dying
microsoft is ok
faceBook is vulnerable

Script::

#!/bin/bash

site="facebook"

awk -v s="$site" 'BEGIN {IGNORECASE = 1} /s/' sites.txt

But the above command does not return any output. How can I use a shell variable inside the begin block to display correct output.

cool_stuff_coming
  • 443
  • 1
  • 6
  • 19
  • Possible duplicate of [How do I use shell variables in an awk script?](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – Shawn Sep 09 '19 at 14:40
  • 1
    You're not trying to use a shell variable inside a BEGIN block, you're trying to use it inside a regular expression. – Ed Morton Sep 09 '19 at 15:07

1 Answers1

4

Your command is literally matching s in every line.

You should be using ~ operator:

awk -v s="$site" 'BEGIN {IGNORECASE = 1} $0 ~ s' sites.txt

Note that IGNORECASE = 1 is a gnu awk feature and if you want a POSIX compliant way then use:

awk -v s="$site" 'tolower($0) ~ tolower(s)' sites.txt
anubhava
  • 761,203
  • 64
  • 569
  • 643