Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I test multiple variables for a single value? 1

Status
Not open for further replies.

leslief

Programmer
Feb 11, 2004
3
US
I have written a script where each record in the input file is delimited into fields by "|". I use cut to put each field into a variable. How do I test all 4 variables for a single value? I want to make sure that all 4 variables are present in the input record (i.e., not = null). Now, I have 4 separate if stmts. There's got to be a better way!

Thanks for any help!

Input record sample:
db|mart|table|column
db|mart|table
db

These last 2 records should be detected as incomplete.

Part of the script:

while read STATINFO
do
ERROR=N
echo ${STATINFO} > stats_wrapper.wrk
DATABASE=`grep "|" stats_wrapper.wrk | cut -d"|" -f1`
MART=`grep "|" stats_wrapper.wrk | cut -d"|" -f2`
TABLE=`grep "|" stats_wrapper.wrk | cut -d"|" -f3`
COLUMN=`grep "|" stats_wrapper.wrk | cut -d"|" -f4`

if [[ ${DATABASE} = "" ]]
then
ERROR=Y
fi

if [[ ${MART} = "" ]]
then
ERROR=Y
fi

if [[ ${TABLE} = "" ]]
then
ERROR=Y
fi

if [[ ${COLUMN} = "" ]]
then
ERROR=Y
fi

if [[ ${ERROR} = @(Y) ]]
echo "INPUT value missing"
else
echo "INPUT OK"
fi

done < ${STATLIST}
 
You can combine your 4 if in only one like this:
if [[ ${DATABASE} = &quot;&quot; || ${MART} = &quot;&quot; || ${TABLE} = &quot;&quot; || ${COLUMN} = &quot;&quot; ]]
then
ERROR=Y
fi
Another way is to use awk to filter the datas:
Code:
awk -F'|' '{
 for(i=1;i<=4;++i)if($i==&quot;&quot; || NF!=4){
  print >&quot;'${STATLIST}.bad'&quot;; next
}}1' ${STATLIST} >${STATLIST}.good

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PHV,

Thanks! Unfortunately, I haven't had the opportunity to use and learn awk yet, so I'll use your other suggestion. From what I'm seeing on this site, I need to get going on learing awk!

Thanks again, leslief
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top