[aclug-L] FW: Searching Files
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
-----Original Message-----
From: Linux_Tips_and_Tricks@xxxxxxxxxxxxxxx
[mailto:Linux_Tips_and_Tricks@xxxxxxxxxxxxxxx]
Sent: Friday, June 21, 2002 12:46 AM
LINUX TIPS AND TRICKS --- June 21, 2002
Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
__________________________________________________________________
HIGHLIGHTS
* This week, Danny shows you how to use the grep and fgrep commands to
search the content of a file for a given pattern.
___________________________________________________________________
Searching Files
By Danny Kalev
Filters
The grep and fgrep are two of many text filters (utilities that scan a
text source and retrieve records that match a given pattern). grep and
fgrep report the file the sought-after pattern was found and display the
lines in which it occurred. The main difference between these two
commands is that grep can search for a single pattern whereas fgrep can
search for multiple patterns at once.
grep
The grep command takes two parameters: the pattern to be searched for
and a list of filenames to be searched. For example, to locate every
line that contains the word 'volatile' in the file simulator.c, type the
following command:
$grep volatile simulator.c
volatile int state;
To include more than one word in the search pattern, enclose the pattern
in single quotes:
$grep 'unsigned long' simulator.c
The quotes are necessary. Otherwise, the grep command will interpret
long as a filename.
When searching in multiple files, grep outputs the name of the file
before the matching line. In the following command, grep searches for
the token EXIT_SUCCESS in the files simulator.c and calendar.c:
$grep EXIT_SUCCESS simulator.c calendar.c
simulator.c: return EXIT_SUCCESS;
calendar.c: if (stat==EXIT_SUCCESS)
Regular Expressions
You may use regular expressions in a search pattern. A regular
expression enables you to locate variations on a general pattern, say
every word that ends in t, or patterns located at different points in
the text, for example, at the end of a line. The following special
characters may be used in regular expressions:
Sign Operation
^ References the beginning of a line
$ References the end of a line
. Matches any possible character
* Matches zero or more characters
[] Matches a set, or a range of characters
Suppose you're reviewing the coding practices of your team and you want
to locate all occurrences of the keyword goto in every source file. The
following command will do the trick:
$grep goto *.c
Now suppose you want to locate every line that ends with { so that you
can move it to the next line. Type the following command:
$grep {$ *.c
simulator.c: struct joystick {
calendar.c: for (n=0; n<MAX; ++n) {
About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more than 10
years of experience, specializing in C++ and object-oriented analysis
and design on various platforms including VMS, DOS, Windows, Unix, and
Linux. His technical interests involve code optimization, networking,
and distributed computing. He is also a member of the ANSI C++
standardization committee and the author of ANSI/ISO C++ Professional
Programmer's Handbook (Que, 1999). Danny can be reached at
linuxnl@xxxxxxxxxx.
____________________________________________________________________
ADDITIONAL RESOURCES
grep
http://itw.itworld.com/GoNow/a14724a60374a76028222a4
grep, egrep, fgrep
http://itw.itworld.com/GoNow/a14724a60374a76028222a1
____________________________________________________________________
ITWORLD.COM NEWSLETTER ARCHIVE
Index of Linux Tips and Tricks
http://itw.itworld.com/GoNow/a14724a60374a76028222a5
Accessing I/O Ports
http://itw.itworld.com/GoNow/a14724a60374a76028222a2
Accessing I/O Ports, Part 2
http://itw.itworld.com/GoNow/a14724a60374a76028222a3
____________________________________________________________________
CUSTOMER SERVICE
SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter
- When finished, click submit
Questions? Please e-mail customer service at: mailto:support@xxxxxxxxxxx
_____________________________________________________________________
CONTACTS
* Editorial: Andrew Santosusso, Newsletter Editor,
andrew_santosusso@xxxxxxxxxxx
* Advertising: Clare O'Brien, Vice President of Sales,
clare_obrien@xxxxxxxxxxx
* Career Corner: Janis Crowley, Vice President/General Manager, IDG
Recruitment Solutions, janis_crowley@xxxxxxxxxxxxx
* Other inquiries: Jodie Naze, Senior Product Marketing Manager,
jodie_naze@xxxxxxxxxxx
_____________________________________________________________________
PRIVACY POLICY
ITworld.com has been TRUSTe certified
http://www.itworld.com/Privacy/
Copyright 2002 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
-- This is the discussion@xxxxxxxxx list. To unsubscribe,
visit http://www.complete.org/cgi-bin/listargate-aclug.cgi
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [aclug-L] FW: Searching Files,
Dale W Hodge <=
|
|