To search multiple string in file

hi ,

i am having a file where i need to take ignore the data from file1.txt and redirect to another file

for eg:

 
file1.txt
AUS
USA
 
file2.txt
AUS,123
NZ,11
USA,12
 

i am using the below code

for i in file1.txt
do
grep -v ^$i file2.txt>out.txt
done

i want to check in grep OR condition from file1.txt with file2.txt

Doesn't

grep -vf file1.txt file2.txt >out.txt

give the wanted result ?

(check the options available depending on your OS implementation of grep )

u can do it this way

cat file1.txt|while read a
do
grep -v ^$i file2.txt > out.txt
done

I would not recommend this because this solution :

  • do a useless use of cat (UUOC)
  • and may lead to duplicate entries since it loops many times on file2.

You have to consider whether it can happen in fil1 that some entries are substring of other entries and may potentially match line that are in file2 but that we don't want to filter out.

Consider for example :

file1 :

US
AUS

file2 :

AUS,123
NZ,11
US,13
USA,12

then something like this may works:

$ sed 's/.*/^&,/' f1 >f1.tmp ; grep -vEf f1.tmp f2 ; rm -f f1.tmp
NZ,11
USA,12

of course it's just an example. It depends on what you may have as input and what you want as output.

But can it happend that the pattern appear somewhere else than at the beginning of the line ?

1 Like

Hi

yes the patten may appear at any of the file but i need to check the first string to be checked only and extact string from file1 needs to be matched
what i actually needed is

input file

AUS,123
NZ,11
US,13
USA,12
USA,12,AUS
AUSA,11,12

output file

NZ,11
USA,12
USA,12,AUS
AUSA,11,12