How to use wildcard * in if?

Hi,

Can anyone help me how to use * in if statement.

File contains below

line1:a|b|c|Apple-RED|
line2:c|d|e|Apple-Green|
line3:f|g|h|Orange|

I need to find line by line 4th field contains 'Apple' or not.

Please help me at the earliest.

 
while read line; do
 
if echo $line|grep '^[^|]*|[^|]*|[^|]*|Apple|'; then
 
fi
 
done <filename

What is your final goal ?

Display lines whose 4th field contains the word "Apple"

awk -F\| '$4~/Apple/' filename

Display lines whose 4th field do not contain the word "Apple"

awk -F\| '$4!~/Apple/' filename

Thanks for your reply.

but i am not getting the output

---------- Post updated at 03:13 PM ---------- Previous update was at 03:11 PM ----------

my need

if 4th field contains Apple i need to run some commands

while using if i am checking the conditions for other columns too

use nawk instead of awk if you are running a SunOS/Solaris plateform

also try

awk -F\| '$4~/Apple/{print}' yourfile
awk -F\| '$4!~/Apple/{print}' yourfile

Don't forget to set FS in awk ...

Alternatively perhaps just shell?

while IFS=\| read f1 f2 f3 f4 f5
do
  case $f4 in 
    *Apple* ) 
       echo "$f1 contains Apple ($f4)"  
  esac
done < infile
1 Like

... Oooops... fixed ! thx