Operating System - HP-UX
1828149 Members
2779 Online
109975 Solutions
New Discussion

Check file existence command

 
ivy1234
Frequent Advisor

Check file existence command

I have a file as below , there is 800 lines in it .
vi file1.txt
aaa
bbb
ccc
ddd
"
"

I have another file as below ( this is a large text file )
vi file2.txt
#fdasfasfasfasfashklfasfbb7x
7fd097af0as78fasfdaaaasgafs
867f9as78f98abbbf96fdsafas


Now , I would like to check each line in the file1.txt whether exists in the file2.txt , that means I would like to know whether aaa exists in file2.txt , and bbb exists in file2.txt , and ccc exists in file2.txt ... or not , I don't want to do it 800 times , can advise what can i do ? thx
10 REPLIES 10
madhuchakkaravarthy
Trusted Contributor

Re: Check file existence command

hi

u can use diff

diff /file1.txt file2.txt

regards

MC
Laurent Menase
Honored Contributor

Re: Check file existence command

you want to see if any string in file1 is in file2
or all string in file1 is in file2?
or which string in file1 is in file2?
Horia Chirculescu
Honored Contributor

Re: Check file existence command

Hello,


#!/usr/bin/sh
echo At the begining of each line -separated by :- there is the line no >results.txt
echo of the file2.txt where the word read from file1.txt was found >>results.txt
echo >>results.txt
for WORD in $(awk '{print $1}' file1.txt)
do
echo For $WORD: >>results.txt
grep -n $WORD file2.txt >>results.txt
echo >>results.txt
done


Best regards,
Horia.
Best regards from Romania,
Horia.
singh sanjeev
Trusted Contributor

Re: Check file existence command

for i in `cat file1.txt`
do
grep -q $i file2.txt
if [ ? = 0 ]
then
echo "$i-------------exits in file"
else
echo "$i-------------do not exist"
fi

it is a small script you can adapt it according to you need.



Sanjeev Singh
Horia Chirculescu
Honored Contributor

Re: Check file existence command

Notice the use of awk! It means that if you want you can add on file1 comments like this:

cat file1.txt

aaa The first word that is to be searched
bbb My son's name. Where on earth is he now?
ccc My wife's name. She's not in the kitchen.
ddd I am starving!

Best regards from Romania
Horia.
Best regards from Romania,
Horia.
ivy1234
Frequent Advisor

Re: Check file existence command

Hi ,

it seems very simple , so I tried it , but I got the error message "line 10: syntax error: unexpected end of file , can advise what is wrong ?


for i in `cat file1.txt`
do
grep -q $i file2.txt
if [ ? = 0 ]
then
echo "$i-------------exits in file"
else
echo "$i-------------do not exist"
fi
Kapil Jha
Honored Contributor

Re: Check file existence command

Lil modification....

for i in `cat file1.txt`
do
grep -q $i file2.txt
if [ $? == 0 ]
then
echo "$i-------------exits in file"
else
echo "$i-------------do not exist"
fi
done

You can supress the second line
>echo "$i-------------do not exist" to
echo ""

BR,
Kapil+
I am in this small bowl, I wane see the real world......
Kapil Jha
Honored Contributor

Re: Check file existence command

OOps it should be
if [ $? = 0 ] insted of
if [ $? == 0 ]


BR,
Kapil+
I am in this small bowl, I wane see the real world......
Sagar Sirdesai
Trusted Contributor

Re: Check file existence command

Hi
Use comm command this is an usefull command to compare files.
use
comm -12 file1.txt and file2.txt

This will list contents which exists in both files

Sagar
Dennis Handly
Acclaimed Contributor

Re: Check file existence command

You could use fgrep -f:
fgrep -f file1.txt file2.txt

>Kapil: it should be if [ $? = 0 ]

No, actually it should be: if [ $? -eq 0 ]

>Sagar: Use comm command this is an useful command to compare files.

This only works if the files are sorted.