Reputation: 65
Purpose: writing a script that takes two parameters (directory and c var) and searches all files with C extension in a directory.
Every line that contains the variable in the second parameter must be printed. The variable should also be matched exactly including capitalization and should be a whole word.
The filename should be printed preceded by the line with the variable
Any help would be appreciated.
Upvotes: 0
Views: 109
Reputation: 12491
I have personally never used ack
so can't comment, but grep
is great for this
I used this command to look for all files containing the whole word "name" with extensions cpp and h in the directory "unisa".
grep -wnr "name" --include *.cpp --include *.h unisa
from the man pages
-w Select only those lines containing matches that form whole words.
-n Prefix each line of output with the 1-based line number within its input file. (you may not want this, but I always use it)
-r Read all files under each directory, recursively
--include Search only files whose base name matches GLOB
I ran the command in the example on a directory I have for all my university projects and this is a sample of the output
cos2614/assignment_1/question_1/question_1.cpp:15: "Enter your details in the format \"first name: surname: employee number\"");
cos2614/assignment_1/question_2/customer.cpp:3:Customer::Customer(QString name, QString id)
cos2614/assignment_1/question_2/customer.cpp:4: : m_Name(name), m_ID(id), m_DeliveryAddress(), m_BillingAddress()
cos2614/assignment_1/question_2/test.cpp:12: QString name, id, address;
cos2614/assignment_1/question_2/test.cpp:14: cout << "Please enter your name: ";
cos2614/assignment_1/question_2/test.cpp:16: name = cin.readLine();
cos2614/assignment_1/question_2/test.cpp:22: Customer cust(name, id);
cos2614/assignment_1/question_2/customer.h:15: Customer(QString name, QString id);
cos2614/assignment_1/question_5/static3.h:10: Client(string name): m_Name(name), m_ID(s_SavedID++) { }
Upvotes: 0
Reputation: 113924
While grep
is a general tool, there is a tool, called ack
, that is specifically designed for searching source code. By default, it recursively searches directory trees. It already has built-in knowledge of which source file extensions are associated with which languages. For example, to search a directory tree for c
files in which the name someName
appears, use:
ack --cc '\bsomeName\b' /path/to/
Notes:
--cc
tells it to search c
-language files as identified by the extensions .c
, .h.
and .xs
.
\bsomeName\b
is the regex to search for. \b
means word boundary. Use it if you want to prevent anothersomeName
from matching. ack
fully supports perl
regular expressions.
Searches are recursive by default. Use -n
if you don't want to descend into subdirectories.
If you don't like any of ack's defaults, it is extremely configurable.
On debian-like systems, ack
was awkwardly renamed ack-grep
to avoid conflict with a Kanji converter called ack
. To install, run apt-get install ack-grep
. For other systems, see How to install ack.
Upvotes: 2
Reputation: 3001
Here, you seem to want to use grep
to search through all of your files in a directory.
Reading man grep
will get you a long way with this, but it's particularly worth paying attention to its --include=GLOB
and -r
or --recursive
options.
You can use the recursive flag to avoid the need for find
, and then the --include=
flag to specify only files ending with a certain extension.
Upvotes: 0