0

I want to write a shell script that cats some file and runs awk to extracts lines that contain a date that I pass into the script as an argument.

The script will be invoked like /my_script.sh 2021-01-01

Here are the contents of /my_script.sh.

DATE=$1
cat /myfile | awk -F , '$12 == "$DATE" { print } '

Note that $12 corresponds to the date column that I care about.

When I run this and look at the process with ps aux, I see that the command being run is literally:

awk -F , $12 == "$DATE" { print }

How can I embed the DATE variable inside the double quotes which is inside the single quotes?

1step1leap
  • 535
  • 1
  • 6
  • 11
  • 2
    Also `awk` is perfectly capable of reading from files itself, without needing them spoon-fed to it by `cat`. So don't use `cat somefile | awk ...`, use `awk ... somefile. – Gordon Davisson Jun 19 '21 at 09:22
  • 1
    I think it could be like this `cat /myfile | awk -v DATE="$DATE" -F , '$12 == DATE'` – The fourth bird Jun 19 '21 at 09:23
  • @GordonDavisson is fine, only use by this `awk -v DATE="$DATE" -F , ...other-awk-parameter /myfile` – Victor Lee Jun 19 '21 at 09:42
  • @VictorLee regarding https://stackoverflow.com/questions/68045314/shell-script-variable-inside-nested-quotes#comment120267941_68045314 - no, `$12 == "$DATE"` tests if `$12` is literally the 5-character string `$DATE`, not the contents of the variable `DATE`. – Ed Morton Jun 19 '21 at 17:19
  • yes, I can’t edit the first comment so i delete that. – Victor Lee Jun 20 '21 at 01:46

0 Answers0