I was trying to use an awk command to verify if a particular column is not matching with a regex (basically I am validating a column in a file with uniform format , if not I need to throw error)
format=$2
col_pos=$1
val= awk -F "|’’ -v m="$format" -v n="$col_pos" '$n ~ "^"m"$"{print $1}' sample_file.txt
if [[ $val != "" ]]; then
echo " column value is having unexpected format"
fi
sh sample.sh [a-z]{8}@gmail.com 3
Awk command is throwing an error. Can anybody help to correct the same?
Input file:
fileid|filename|contactemail
1|file1.txt|src@gmail.com
2|file2.txt|rec@gmail.com
3|file3.txt|xyz -------->invalid column value as it doesnt satisfies the format @gmail.com
Here is the sample program run (expected to catch error as xyz is not a valid email)
$ sh sample.sh 3 [a-z]@gmail.com
$ sh -x sample.sh 3 [a-z]@gmail.com
+ format='[a-z]@gmail.com'
+ col_pos=3
++ awk -F '~' -v 'm=[a-z]@gmail.com' -v n=3 '$n ~ "^"m"$"{print $1}' sample_file.txt
+ val=
+ [[ '' != '' ]]
awkthrowing? – tink May 11 '21 at 22:49filenamelook like? Please edit the question with the extra information ... – tink May 11 '21 at 22:53=~should be just~and (2)^and$in the computed regex need to be string constants i.e.$n ~ "^" m "$". There are additional issues at the shell level. – steeldriver May 11 '21 at 23:00val, owing to the space after the=sign. Really you should not be using "bacticks" at all (they are deprecated), use$(...)instead, soval=$(awk ...). Also your actual script appears to still use the wrong field separator (-F '~'rather than-F '|'to match your sample data). – steeldriver May 12 '21 at 00:17