![]() ![]() -context (or -C) displays a specified number of lines before and after your matchįor example, to see two lines before a matched pattern: $ grep Baz -B2 metasyntactic.list.-before-context (or -B) displays a specified number of lines before your match.-after-context (or -A) displays a specified number of lines after your match.There’s a trio of options for doing this, and they’re as easy to remember as A-B-C (literally): For example: $ grep -only-matching -line-number Fedora example.txtĪ common way to get context about how-or why-a pattern appears in a file is to view the line above the match, or the line just after it, or both. For added context, use the -line-number option ( -n for short) to see the line number where the matched pattern appears in the file. The -only-matching (or -o for short) grep option prints only the matching part of a line. If your text file has extra long lines, then your results can contain a lot more data than you anticipate since grep doesn’t lift the string out of context. Yet, you can use a command such as strings to extract just the binary file’s plain text, and then use grep on the results like this: $ strings example.xcf | grep gimpĪ line of text is considered a string of characters terminating with a new line character-specifically, 0x0D0A or \r\n, the carriage return (CR) and line feed (LF) ASCII characters respectively. For instance, you can’t normally grep through a binary file, since binary files don’t contain much raw text. This tactic also enables grep to be used in situations when it otherwise might not be effective.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |