grep.texi

来自「linux平台中」· TEXI 代码 · 共 1,814 行 · 第 1/5 页

TEXI
1,814
字号
grep 'hello\>' *@end example@noindentsearches only for words ending in @samp{hello}, so it matches the word@samp{Othello}.@itemHow do I output context around the matching lines?@examplegrep -C 2 'hello' *@end example@noindentprints two lines of context around each matching line.@itemHow do I force grep to print the name of the file?Append @file{/dev/null}:@examplegrep 'eli' /etc/passwd /dev/null@end examplegets you:@smallexample/etc/passwd:eli:DNGUTF58.IMe.:98:11:Eli Smith:/home/do/eli:/bin/bash@end smallexample@itemWhy do people use strange regular expressions on @command{ps} output?@exampleps -ef | grep '[c]ron'@end exampleIf the pattern had been written without the square brackets, it wouldhave matched not only the @command{ps} output line for @command{cron},but also the @command{ps} output line for @command{grep}.Note that some platforms @command{ps} limit the ouput to the widthof the screen, grep does not have any limit on the length of a lineexcept the available memory.@itemWhy does @command{grep} report ``Binary file matches''?If @command{grep} listed all matching ``lines'' from a binary file, itwould probably generate output that is not useful, and it might evenmuck up your display.  So @sc{gnu} @command{grep} suppresses output fromfiles that appear to be binary files.  To force @sc{gnu} @command{grep}to output lines even from files that appear to be binary, use the@samp{-a} or @samp{--binary-files=text} option.  To eliminate the``Binary file matches'' messages, use the @samp{-I} or@samp{--binary-files=without-match} option.@itemWhy doesn't @samp{grep -lv} print nonmatching file names?@samp{grep -lv} lists the names of all files containing one or morelines that do not match.  To list the names of all files that contain nomatching lines, use the @samp{-L} or @samp{--files-without-match}option.@itemI can do @sc{or} with @samp{|}, but what about @sc{and}?@examplegrep 'paul' /etc/motd | grep 'franc,ois'@end example@noindentfinds all lines that contain both @samp{paul} and @samp{franc,ois}.@itemHow can I search in both standard input and in files?Use the special file name @samp{-}:@examplecat /etc/passwd | grep 'alain' - /etc/motd@end example@item@cindex palindromesHow to express palindromes in a regular expression?It can be done by using the back referecences, for example a palindromeof 4 chararcters can be written in BRE.@examplegrep -w -e '\(.\)\(.\).\2\1' file@end exampleIt matches the word "radar" or "civic".Guglielmo Bondioni proposed a single RE that finds all the palindromes up to 19characters long.@exampleegrep -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file@end exampleNote this is done by using GNU ERE extensions, it might not be portable onother greps.@itemWhy are my expressions whith the vertical bar fail?@example/bin/echo "ba" | egrep '(a)\1|(b)\1'@end exampleThe first alternate branch fails then the first group was not in the matchthis will make the second alternate branch fails.  For example, "aaba" willmatch, the first group participate in the match and can be reuse in thesecond branch.@itemWhat do @command{grep, fgrep, egrep} stand for ?grep comes from the way line editing was done on Unix.  For example,@command{ed} uses this syntax to print a list of matching lines on the screen.@exampleglobal/regular expression/printg/re/p@end example@command{fgrep} stands for Fixed @command{grep}, @command{egrep} Extended@command{grep}.@end enumerate@node Reporting Bugs, Copying, Usage, Top@chapter Reporting bugs@cindex Bugs, reportingEmail bug reports to @email{bug-gnu-utils@@gnu.org}.Be sure to include the word ``grep'' somewhere in the ``Subject:'' field.Large repetition counts in the @samp{@{n,m@}} construct may cause@command{grep} to use lots of memory.  In addition, certain otherobscure regular expressions require exponential time andspace, and may cause grep to run out of memory.Back-references are very slow, and may require exponential time.@node Copying, GNU General Public License, Reporting Bugs, Top@chapter Copying@cindex CopyingGNU grep is licensed under the GNU GPL, which makes it @dfn{freesoftware}.Please note that ``free'' in ``free software'' refers to liberty, notprice.  As some GNU project advocates like to point out, think of ``freespeech'' rather than ``free beer''.  The exact and legally bindingdistribution terms are spelled out below; in short, you have the right(freedom) to run and change grep and distribute it to other people, andeven---if you want---charge money for doing either.  The importantrestriction is that you have to grant your recipients the same rightsand impose the same restrictions.This method of licensing software is also known as @dfn{open source}because, among other things, it makes sure that all recipients willreceive the source code along with the program, and be able to improveit.  The GNU project prefers the term ``free software'' for reasonsoutlined at@url{http://www.gnu.org/philosophy/free-software-for-freedom.html}.The exact license terms are defined by this paragraph and the GNUGeneral Public License it refers to:@quotationGNU grep is free software; you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation; either version 2 of the License, or (at youroption) any later version.GNU grep is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public Licensefor more details.A copy of the GNU General Public License is included as part of thismanual; if you did not receive it, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.@end quotationIn addition to this, this manual is free in the same sense:@quotationPermission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.1 orany later version published by the Free Software Foundation; with theInvariant Sections being ``GNU General Public License'' and ``GNU FreeDocumentation License'', with no Front-Cover Texts, and with noBack-Cover Texts.  A copy of the license is included in the sectionentitled ``GNU Free Documentation License''.@end quotation@c #### Maybe we should wrap these licenses in ifinfo?  Stallman says@c that the GFDL needs to be present in the manual, and to me it would@c suck to include the license for the manual and not the license for@c the program.The full texts of the GNU General Public License and of the GNU FreeDocumentation License are available below.@menu* GNU General Public License:: GNU GPL* GNU Free Documentation License:: GNU FDL@end menu@node GNU General Public License, GNU Free Documentation License, Copying, Copying@section GNU General Public License@center Version 2, June 1991@cindex GPL, GNU General Public License@displayCopyright @copyright{} 1989, 1991 Free Software Foundation, Inc.675 Mass Ave, Cambridge, MA 02139, USAEveryone is permitted to copy and distribute verbatim copiesof this license document, but changing it is not allowed.@end display@unnumberedsec Preamble  The licenses for most software are designed to take away yourfreedom to share and change it.  By contrast, the GNU General PublicLicense is intended to guarantee your freedom to share and change freesoftware---to make sure the software is free for all its users.  ThisGeneral Public License applies to most of the Free SoftwareFoundation's software and to any other program whose authors commit tousing it.  (Some other Free Software Foundation software is covered bythe GNU Library General Public License instead.)  You can apply it toyour programs, too.  When we speak of free software, we are referring to freedom, notprice.  Our General Public Licenses are designed to make sure that youhave the freedom to distribute copies of free software (and charge forthis service if you wish), that you receive source code or can get itif you want it, that you can change the software or use pieces of itin new free programs; and that you know you can do these things.  To protect your rights, we need to make restrictions that forbidanyone to deny you these rights or to ask you to surrender the rights.These restrictions translate to certain responsibilities for you if youdistribute copies of the software, or if you modify it.  For example, if you distribute copies of such a program, whethergratis or for a fee, you must give the recipients all the rights thatyou have.  You must make sure that they, too, receive or can get thesource code.  And you must show them these terms so they know theirrights.  We protect your rights with two steps: (1) copyright the software, and(2) offer you this license which gives you legal permission to copy,distribute and/or modify the software.  Also, for each author's protection and ours, we want to make certainthat everyone understands that there is no warranty for this freesoftware.  If the software is modified by someone else and passed on, wewant its recipients to know that what they have is not the original, sothat any problems introduced by others will not reflect on the originalauthors' reputations.  Finally, any free program is threatened constantly by softwarepatents.  We wish to avoid the danger that redistributors of a freeprogram will individually obtain patent licenses, in effect making theprogram proprietary.  To prevent this, we have made it clear that anypatent must be licensed for everyone's free use or not licensed at all.  The precise terms and conditions for copying, distribution andmodification follow.@iftex@unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION@end iftex@ifinfo@center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION@end ifinfo@enumerate@itemThis License applies to any program or other work which containsa notice placed by the copyright holder saying it may be distributedunder the terms of this General Public License.  The ``Program'', below,refers to any such program or work, and a ``work based on the Program''means either the Program or any derivative work under copyright law:that is to say, a work containing the Program or a portion of it,either verbatim or with modifications and/or translated into anotherlanguage.  (Hereinafter, translation is included without limitation inthe term ``modification''.)  Each licensee is addressed as ``you''.Activities other than copying, distribution and modification are notcovered by this License; they are outside its scope.  The act ofrunning the Program is not restricted, and the output from the Programis covered only if its contents constitute a work based on theProgram (independent of having been made by running the Program).Whether that is true depends on what the Program does.@itemYou may copy and distribute verbatim copies of the Program'ssource code as you receive it, in any medium, provided that youconspicuously and appropriately publish on each copy an appropriatecopyright notice and disclaimer of warranty; keep intact all thenotices that refer to this License and to the absence of any warranty;and give any other recipients of the Program a copy of this Licensealong with the Program.You may charge a fee for the physical act of transferring a copy, andyou may at your option offer warranty protection in exchange for a fee.@itemYou may modify your copy or copies of the Program or any portionof it, thus forming a work based on the Program, and copy anddistribute such modifications or work under the terms of Section 1above, provided that you also meet all of these conditions:@enumerate a@itemYou must cause the modified files to carry prominent noticesstating that you changed the files and the date of any change.@itemYou must cause any work that you distribute or publish, that inwhole or in part contains or is derived from the Program or anypart thereof, to be licensed as a whole at no charge to all thirdparties under the terms of this License.@itemIf the modified program normally reads commands interactivelywhen run, you must cause it, when started running for suchinteractive use in the most ordinary way, to print or display anannouncement including an appropriate copyright notice and anotice that there is no warranty (or else, saying that you providea warranty) and that users may redistribute the program underthese conditions, and telling the user how to view a copy of thisLicense.  (Exception: if the Program itself is interactive butdoes not normally print such an announcement, your work based onthe Program is not required to print an announcement.)@end enumerateThese requirements apply to the modified work as a whole.  Ifidentifiable sections of that work are not derived from the Program,and can be reasonably considered independent and separate works inthemselves, then this License, and its terms, do not apply to thosesections when you distribute them as separate works.  But when youdistribute the same sections as part of a whole which is a work basedon the Program, the distribution of the whole must be on the terms ofthis License, whose permissions for other licensees extend to theentire whole, and thus to each and every part regardless of who wrote it.Thus, it is not the intent of this section to claim rights or contestyour rights to work written entirely by you; rather, the intent is toexercise the right to control the distribution of derivative orcollective works based on the Program.In addition, mere aggregation of another work not based on the Programwith the Program (or with a work based on the Program) on a volume of

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?