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,114 questions
3 votes
3 answers
871 views
How do I ensure a bash script argument is a specific value?
The following script is supposed to check $1 and see if it is a specific value. It only works when one Bash [[... =~ ...]] regex check is passed in an if statement. The error is: When more than one ...
8 votes
1 answer
1k views
How and why does grep match this pattern (containing newline)?
Consider this scenario: $ x='yyy ' $ printf %s "$x" | od -tc 0000000 y y y \n 0000004 $ printf %s "xxx" | od -c 0000000 x x x 0000003 $ printf %s "xxx" | ...
1 vote
2 answers
129 views
find awk grep - search and replace & passing modified contents to awk to overwrite the existing file
I have a folder with many subfolders full of various Quarto(reg) files & in those files there are links that are located in varying positions in the file lines. UPDATE ON 3 November 2025 in ...
7 votes
9 answers
699 views
How to get every lines between nth and (n+1)th match of grep in text file
I've got a text file containing e.g. Success Something Anything Success Somebody Anybody Someone Success (line 8 is deliberately an empty line) and I would like to export every line between the nth ...
2 votes
4 answers
838 views
How to use a "grep" result as an input of another grep, resulting in multiple lines?
I have built a Visual Studio solution, containing 92 projects. While building them, I get the following line for every one of them: 10>------ Rebuild All started: Project: HostLinkSw, Configuration:...
0 votes
3 answers
160 views
How to construct a command from the output of another command?
I'm working with a Linux application. There is a command that I run to display a set of parameters. I would like to run the command and then grep through the results of the command and build a ...
10 votes
5 answers
605 views
Grep with output of not the complete line
With grep I can filter lines. But if the lines are pretty long it gets messy. How can I only get "some chars around" my search-string? f.txt this is a red cat in the room this is a blue ...
3 votes
2 answers
119 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
69 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
143 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
525 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
601 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
378 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
821 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
129 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 ...