I have the following bash script:
#!/bin/bash
OUTPUT=NULL,2,3,NULL,5,6,7,8
pvar1=$(echo $OUTPUT | awk -F ',' '{print $1}')
pvar2=$(echo $OUTPUT | awk -F ',' '{print $2}')
pvar3=$(echo $OUTPUT | awk -F ',' '{print $3}')
pvar4=$(echo $OUTPUT | awk -F ',' '{print $4}')
pvar5=$(echo $OUTPUT | awk -F ',' '{print $5}')
pvar6=$(echo $OUTPUT | awk -F ',' '{print $6}')
pvar7=$(echo $OUTPUT | awk -F ',' '{print $7}')
pvar8=$(echo $OUTPUT | awk -F ',' '{print $8}')
for i in {$pvar1..$pvar8}
do
case "$i" in
"NULL")
pOUT="$(awk '{ $* = $* }1' FS=, OFS=, <<<"$OUTPUT")"
;;
"")
echo "particulars field is not allowed to be empty. Run the program again and enter NULL or choose a value!" && exit 0
;;
*)
pOUT="$(awk '{ $* = q $* q }1' FS=, OFS=, q="'" <<<"$OUTPUT")"
;;
esac
done
echo "$pOUT"
The output should be:
pOUT=NULL,'2','3',NULL,'5','6','7','8'
And the script should exit if no value is provided, e.g.:
OUTPUT=NULL,2,3,NULL,5,6,,8
I am not sure how to write the awk commands in a for loop. Can someone please help?
Best Answer
Running awk to split one(!) field or join characters is wasteful and unnecessary; shell can do this fine. It's not clear exactly what you want because some of your code makes no sense at all, but based on your single example this should be close:
but if you do want to use awk it can easily do the whole job at once
or even easier, perl: