mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-09 05:44:23 +00:00
Added grep notes to the knowledge base
This commit is contained in:
41
knowledge base/grep the standard error stream.md
Normal file
41
knowledge base/grep the standard error stream.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Grep the standard error stream
|
||||
|
||||
If you're using `bash` or `zsh` you can employ anonymous pipes:
|
||||
|
||||
```shell
|
||||
ffmpeg -i 01-Daemon.mp3 2> >(grep -i Duration)
|
||||
```
|
||||
|
||||
If you want the filtered redirected output on `stderr` again, add the `>&2` redirection to grep:
|
||||
|
||||
```shell
|
||||
command 2> >(grep something >&2)
|
||||
```
|
||||
|
||||
`2>` redirects `stderr` to a pipe, while `>(command)` reads from it. This is _syntactic sugar_ to create a pipe (not a file) and remove it when the process completes. They are effectively anonymous, because they are not given a name in the filesystem.
|
||||
Bash calls this _process substitution_:
|
||||
|
||||
> Process substitution can also be used to capture output that would normally go to a file, and redirect it to the input of a process.
|
||||
|
||||
You can exclude `stdout` and grep `stderr` redirecting it to `null`:
|
||||
|
||||
```shell
|
||||
command 1>/dev/null 2> >(grep -oP "(.*)(?=pattern)")
|
||||
```
|
||||
|
||||
> Do note that **the target command of process substitution runs asynchronously**.
|
||||
> As a consequence, `stderr` lines that get through the grep filter may not appear at the place you would expect in the rest of the output, but even on your next command prompt.
|
||||
|
||||
## See also
|
||||
|
||||
- Knowledge base on [grep]
|
||||
|
||||
## Further readings
|
||||
|
||||
- Answer on [StackExchange] about [how to grep the standard error stream]
|
||||
|
||||
[grep]: grep.md
|
||||
|
||||
[stackexchange]: https://unix.stackexchange.com
|
||||
|
||||
[how to grep the standard error stream]: https://unix.stackexchange.com/questions/3514/how-to-grep-standard-error-stream-stderr/#3657
|
||||
54
knowledge base/grep.md
Normal file
54
knowledge base/grep.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# Grep
|
||||
|
||||
## TL;DR
|
||||
|
||||
```shell
|
||||
# base search
|
||||
grep 'pattern' path/to/search
|
||||
|
||||
# recursive search
|
||||
grep -R 'pattern' path/to/search/recursively
|
||||
grep -R --exclude-dir excluded/dir 'pattern' path/to/search/recursively # gnu grep >= 2.5.2
|
||||
|
||||
# show line numbers
|
||||
grep -n 'pattern' path/to/search
|
||||
```
|
||||
|
||||
## Grep variants
|
||||
|
||||
- [`egrep`](#egrep) to use regular expressions in search patterns, same as `grep -E`
|
||||
- [`fgrep`](#fgrep) to use patterns as fixed strings, same as `grep -F`
|
||||
- [archive-related variants](#archive-related-variants) for searching into compressed files
|
||||
- [`pdfgrep`](#pdfgrep) for searching into PDF files
|
||||
|
||||
### Archive-related variants
|
||||
|
||||
- [`xzgrep`](#xzgrep) (with `xzegrep` and `xzfgrep`)
|
||||
- [`zstdgrep`](#zstdgrep) for zstd archives
|
||||
- many many others
|
||||
|
||||
### PDFgrep
|
||||
|
||||
For simple searches, you might want to use [pdfgrep].
|
||||
|
||||
Should you need more advanced grep capabilities not incorporated by pdfgrep, you might want to convert the file to text and search there.
|
||||
You can to this using [`pdftotext`](pdfgrep.md) as shown in this example ([source][stackoverflow answer about how to search contents of multiple pdf files]):
|
||||
|
||||
```sh
|
||||
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' ';'
|
||||
```
|
||||
|
||||
## Further readings
|
||||
|
||||
- Answer on [StackOverflow] about [how to search contents of multiple pdf files]
|
||||
- [Regular expressions in grep with examples]
|
||||
- [Grep the standard error stream]
|
||||
- Knowledge base on [pdfgrep]
|
||||
|
||||
[grep the standard error stream]: grep\ the\ standard\ error\ stream.md
|
||||
[pdfgrep]: pdfgrep.md
|
||||
|
||||
[stackoverflow]: https://stackoverflow.com
|
||||
|
||||
[how to search contents of multiple pdf files]: https://stackoverflow.com/a/4643518
|
||||
[regular expressions in grep with examples]: https://www.cyberciti.biz/faq/grep-regular-expressions/
|
||||
Reference in New Issue
Block a user