Home > OS >  checking the right info from a txt under conditions using bashscript
checking the right info from a txt under conditions using bashscript

Time:06-20

I have a text file text.txt having the info :

nesha [email protected] 311
neco 244
doda [email protected] 333
nicol [email protected]
boter boter@nginx 220

and I wanna check using bash script all the users if the domain name has ".com" and if it's valid then check their ID (ID is column 3) if it is odd or even and tell me and if no domain name just continue the next line.

input="text.txt"
regex='.com'
while read -r _ rec_column2 rec_column3
do
  if [[ $rec_column2 =~ $regex ]] || [[ $((rec_column3 % 2)) -eq 0 ]]
  then 
      echo "$rec_column3 IS valid and even";
  elif [[ $rec_column2 =~ $regex ]] || [[ $((rec_column3 % 2)) -eq 1 ]]
  then 
      echo "$rec_column3 IS valid and odd";
  elif [[ -z "$rec_column3" ]]
  then
      continue
  else
      echo "$rec_column3 IS NOT valid"
  fi
done < $input

output :

311 IS valid and even
 IS valid and even
333 IS valid and even
 IS valid and even
220 IS valid and even

so this is my script I used regex to check the domain name and it's working but the script not checking if the number is odd or even or not skipping the line if not have the right domain, so any help? tnx

CodePudding user response:

I hope I understood the task, can you see if this helps?

input="text.txt"
regex='.com'
while read -r _ rec_column2 rec_column3
do
    if [[ -z $rec_column3 ]]
    then
        continue
    fi
    if [[ $rec_column2 =~ $regex ]]
    then
      if [[ $((rec_column3 % 2)) -eq 0 ]]
      then
        echo "$rec_column3 IS valid and even";
      else
        echo "$rec_column3 IS valid and odd";
      fi
    else
      echo "$rec_column3 IS NOT valid"
    fi
done < $input

I've changed the order of the conditions to first check if column 3 is not empty, then checking if the email is valid and only then check if the number is even or odd

  • Related