- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Check file existence command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 10:45 PM
06-03-2010 10:45 PM
Check file existence command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 11:12 PM
06-03-2010 11:12 PM
Re: Check file existence command
u can use diff
diff /file1.txt file2.txt
regards
MC
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 11:19 PM
06-03-2010 11:19 PM
Re: Check file existence command
or all string in file1 is in file2?
or which string in file1 is in file2?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 11:24 PM
06-03-2010 11:24 PM
Re: Check file existence command
#!/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.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 11:25 PM
06-03-2010 11:25 PM
Re: Check file existence command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2010 11:31 PM
06-03-2010 11:31 PM
Re: Check file existence command
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.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2010 12:13 AM
06-04-2010 12:13 AM
Re: Check file existence command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2010 01:13 AM
06-04-2010 01:13 AM
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
done
You can supress the second line
>echo "$i-------------do not exist" to
echo ""
BR,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2010 01:15 AM
06-04-2010 01:15 AM
Re: Check file existence command
if [ $? = 0 ] insted of
if [ $? == 0 ]
BR,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2010 03:21 AM
06-04-2010 03:21 AM
Re: Check file existence command
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
- Tags:
- comm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2010 07:05 AM - edited 10-01-2011 07:42 PM
06-04-2010 07:05 AM - edited 10-01-2011 07:42 PM
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.