Skip to content
Advertisement

Assignment of variables in shell scripting

I am trying to write a shell script to simulate the following problem:

File A contains some entries like

a  
b  
c 

and File B contains

a  
g  
d  
t  
q  
c  

the final file should have
(contents of file b which are not there in file a)

g  
d  
t  
q  

I was trying something like this but it’s not working:

for i in `cat b` ; do if[ (grep $i a) != $i ] then echo $i; done

Advertisement

Answer

You code has some errors:

  1. forgot the closure of if statement : fi

  2. “grep $i a” must be interpreted as a command , such as $(grep $i a) or `grep $i a`

  3. You must add some characters around $(grep $i a) avoid empty string comparasion , such as “:” or something else, whatever.

Solution:

for i in `cat b` ; do if [ :$(grep $i a) != :$i ] ;then echo $i;fi; done
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement