Finding non-existing words in a list of files in a directory and its sub-directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding non-existing words in a list of files in a directory and its sub-directories
# 1  
Old 01-12-2016
Finding non-existing words in a list of files in current directory and/or its sub-directories

Hi All,

I have a list of words (these are actually a list of database table names separated by comma).

Now, I want to find only the non-existing list of words in the *.java files of current directory and/or its sub-directories.

Sample list of words: table_one,table_ten,table_x,table_y,table_z

Type of files in current directory and/or its sub-directories: *.java

Suppose, 2 words (say table_ten and table_z) are not exist in any of the *.java files in current directory and/or its sub-directories, then print these 2 words as output as shown below.

Non-existing words are: table_ten,table_z

Please help.

Thanks in advance.

Last edited by Bhanu Dhulipudi; 01-12-2016 at 05:38 AM..
# 2  
Old 01-12-2016
1. What shell/OS are you using?
2. What have you tried? (It's easier and quicker for forum members to help you from where you're stuck; rather than give you a solution they have to work from scratch, just for you)
3. To help you get started, you could try a combination of find and grep. Let us know what you could come up with.
# 3  
Old 01-12-2016
@balajesuri Thanks for your reply :-)

I am using bash shell.

I guess, this can be done with shell scripting using a for loop.

Last edited by Bhanu Dhulipudi; 01-12-2016 at 06:26 AM..
# 4  
Old 01-12-2016
A crude and inefficient way:
Code:
for file in $(find /path/to/dir -type f -name "*java")
do
    for word in table_one table_two table_three
    do
        if grep -q $word $file
        then
            continue
        else
           echo "$file does not contain $word"
        fi
    done
done


Last edited by balajesuri; 01-12-2016 at 07:22 AM.. Reason: syntax correction
# 5  
Old 01-12-2016
Try (untested, assuming table names in a one line file):
Code:
awk -F, '
NR == 1         {for (n=split($0, T); n>0; n--) TBL[T[n]]=n
                 next
                }
$0 in TBL       {delete TBL[$0]
                }
END             {print "non-existing:"
                 for (t in TBL) print t
                }
' tblfile *.java

# 6  
Old 01-12-2016
@RudiC Thanks for your reply :-)

After ran the script code, got the below error message.

bash-3.2$ ./test.sh
Syntax Error The source line is 1.
The error context is
<<< >>>
awk: 0602-500 Quitting The source line is 1.
bash-3.2$
# 7  
Old 01-12-2016
What files did you supply to the script?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

List all files and directories in the current directory separated by commas and sorted by crtime

What I know so far: ls -A will list all files except those starting with a dot ls -d will list all directories ls -m will separate contents by commas For getting crtimes use: stat filename will give me the inode number or ls -i filename will give... (13 Replies)
Discussion started by: chstewar
13 Replies

2. Shell Programming and Scripting

Finding files deep in directories

i need to find a portable way to go through multiple directories to find a file. I've trid something like this: find /opt/oracle/diag/*/alert_HH2.log -printordinarily, i can run the ls command and it will find it: /opt/oracle/diag/*/*/*/*/alert_HH2.log The problem with this approach is... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Finding the Files In the Same Name Directories

Hi, In the Unix Box, I have a situation, where there is folder name called "Projects" and in that i have 20 Folders S1,S2,S3...S20. In each of the Folders S1,S2,S3,...S20 , there is a same name folder named "MP". So Now, I want to get all the files in all the "MP" Folders and write all those... (6 Replies)
Discussion started by: Siva Sankar
6 Replies

5. UNIX for Dummies Questions & Answers

Appending lines from an existing list to each line in another existing list

Evening all ! I would like to ask your expertise on how to accomplish the following ; I have 2 lists, and would like each line from list2 to be appended to each line in list1, resulting in list3 ; List1; alpha beta charlie List2; one two three (4 Replies)
Discussion started by: TAPE
4 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

8. Shell Programming and Scripting

Finding directory and sub-directories individual size in Perl

Hi, Can anyone redirect to an existing thread or provide some info on how to find the size of a directory and it's sub-directories using a single script ? I tried finding a similar thread but in vain. I'm a newbie and any help would be greatly appreciated. Thanks in advance. (3 Replies)
Discussion started by: ryder
3 Replies

9. UNIX for Dummies Questions & Answers

List files that are not directories from current directory

I can't manage to list all files that are not directories from current directory. (2 Replies)
Discussion started by: beni22sof
2 Replies

10. UNIX for Dummies Questions & Answers

finding largest files (not directories)?

hello all. i would like to be able to find the names of all files on a remote machine using ssh. i only want the names of files, not directories so far i'm stuck at "du -a | sort -n" also, is it possible to write them to a file on my machine? i know how to write it to a file on that... (2 Replies)
Discussion started by: user19190989
2 Replies
Login or Register to Ask a Question