Campdave
Campdave

Reputation: 3

Trying to pass a paramater from a shell script to awk

I am trying to pass a parameter to this script named HandleError.sh:

#!/bin/ksh
s=$1
echo $s
awk -v search=$s '$0 ~ /search/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; 
i<=NR ;  i++)     
print arr[i]  }' W_ERP_CLINICAL_LOAD.out > ENCOUNTER_DETAIL_ERROR.txt

I am calling it like this:

HandleError.sh BEGIN EMR_LAB_FAC  

I am trying to copy the file W_ERP_CLINICAL_LOAD.out from the first occurrence of BEGIN EMR_LAB_FAC to the bottom of the file. Everything is working well except passing the parameter.

I can hard code like this and it works fine.

awk '$0 ~ /BEGIN ENCOUNTER_DETAIL/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ;  
i++) print arr[i]  }' W_ERP_CLINICAL_LOAD.out > ENCOUNTER_DETAIL_ERROR.txt

Any ideas?

Upvotes: 0

Views: 72

Answers (2)

Ed Morton
Ed Morton

Reputation: 203129

You want this:

awk -v search="$s" '$0 ~ search {f=1} f' W_ERP_CLINICAL_LOAD.out

no need for an array. Also the approach you had would copy the entire contents of the file if the search pattern was not found.

Upvotes: 1

johnsyweb
johnsyweb

Reputation: 141770

Similar to this answer...

Replace $0 ~ /search/ with $0 ~ search.

Upvotes: 2

Related Questions