Questions tagged [grep]
For questions pertaining to grep, a command-line tool for searching text patterns in files. Use this tag for questions about grep itself or questions about issues arising from using the grep command-line tool.
5,107 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
3
votes
2
answers
94
views
Bug or not?: grep stops finding more matches after "Binary file ... matches"
I have a longer log file, and I wanted to extract the lines that match a specific word using grep.
However after a few thousand lines with matches, grep output
Binary file ... matches
and that was ...
-1
votes
1
answer
62
views
How do I grep for "-d"? [duplicate]
I'm trying to search a directory of files for occurences of the literal string -d /data/. When I try using grep, I get an error due to the fact that -d is a grep flag:
$ grep -F "-d /data" ...
-2
votes
1
answer
137
views
sed inplace in selective block
I have a dir containing logstash config files
these files have filter blocks, and sometimes nested child blocks
need to match whole filter block
filter { any text till final matching closing brace for ...
4
votes
3
answers
518
views
In Grep, how can I `grep -r --exclude build/lib/**/*.py`
I setup a directory structure as follows:
mkdir -p /tmp/test/build/lib/aaa/
cd /tmp/test
mkdir rar
echo "Hello" > foo.py
echo "Hello" > bar.py
echo "Hello" > ...
8
votes
1
answer
594
views
Is there any way to see the string that was matched in grep?
I'm not talking about -o option.
Posix says:
The search for a matching sequence starts at the beginning of a string and stops when the first sequence matching the expression is found, where "...
3
votes
1
answer
369
views
How to do non-greedy multiline capture with recent versions of pcre2grep?
I noticed a difference in behavior between an older pcre2grep version (10.22) and a more recent one (10.42), and I am wondering how I can get the old behavior back.
Take the following file:
aaa
bbb
...
0
votes
1
answer
776
views
grep behaviour is different when run using bash -c '...'
I met an interesting issue while working with this code from Stack Overflow: tripleee's answer on "How to check if a file contains only zeros in a Linux shell?"
Why does the same bash code ...
1
vote
2
answers
123
views
How to extract a sub-heading as string which is above a search for word
I'm new to Bash and I've been self-taught. I think I'm learning well, but I do have staggering gaps in my base knowledge. So sorry if this is woefully simple bbuuuttt...
Essentially, I need to sift ...
4
votes
1
answer
398
views
Is there a way to set up default values for grep options such as `--exclude-dir`?
I often have to use the --exclude-dir to exclude various folders such as .git from the search path.
grep -r --exclude-dir=.git --exclude-dir=another_path XXX
Then, the command line becomes quite ...
0
votes
0
answers
27
views
How can I grep the output of ffprobe? [duplicate]
I'd like to only see those lines containing "Stream #" from an ffprobe output. But whatever I do, it continues to show the whole output. Neither "|" nor ">" pipes work....
0
votes
1
answer
117
views
How to match exact string? [duplicate]
I tried this
grep -rn "application_config_project" .
I got many
application_config_project_name = f"{app_acronym}-application-config"
github_application_config_repo = ...
2
votes
1
answer
84
views
Redirect `rtf` output to file
System Info
alinuxchap@libertus-desktop:/usr/share/X11/xkb $ uname -a
Linux libertus-desktop 6.12.25+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025年04月30日) aarch64 GNU/Linux
alinuxchap@...
3
votes
2
answers
338
views
Programs to visually verify the placement of null bytes by grep [duplicate]
I have been using the grep command with --null.
grep --null -r -n -- "$pstr" "$pdir"
The man page for my version of grep (on Mac OS Sequoia) says:
--null Prints a zero-byte ...
0
votes
1
answer
127
views
grep command not finding text in MS-Office or PDF documents
Debian GNU/Linux 11 (bullseye), grep (GNU grep) 3.6
I need find string in current directory within all files (doc, docx and pdf), grep command not working for me:
grep -ril "word" .
It ...
2
votes
4
answers
170
views
Grep (BRE) on surrounding delimiters w/o consuming the delimiter? Counting delimiter-separated strings between filename and extension
I have a dataset of images labeled/classified by characteristics, where an image can have more than one label. I want to count how many of each identifier I have. A toy dataset is created below, with ...