Grep Search And Replace in Mass Files - grep

Even though there are lots of grep questions and answers, these don't answer and I need help in this. I need to make
Title-BEX-override-8>"
expressions to become
Title-BEX>"
Any letters or words among Title-BEX and >" should be terminated. I need an exact grep expression for this.
And some optional answers can be about this: I want to do is thin multiple files. And prefer doing this in Mac.

grep cannot do text replacement.
try sed
sed 's/Title-BEX-override-8/Title-BEX/g' file
-i option can let you do it "in place". but I don't know the corresponding option is for your sed on mac.. :(

Related

GREP: Find words containing multiple specific characters

To clarify a bit, while I am aware how to grab words with a single specific character, I'm unsure how to approach looking for multiple of them. For example, what grep command would be used to retrieve only the words containing both "b" and "p" (in any order), not just one or the other?
Using the above example, if you're given words like "bear," "pear," "biography," and "printable," it would only return the last two words. These are some of my previous attempts.
grep -E "\b[bp]\b" input
grep -E "\b(b|p)\b" input
grep -E "\bb.*p\b" input
you can do it with a regular expression. For instance, here the code snippet for your problem.
grep '\w*[b]\w*[p]\w*\|\w*[p]\w*[b]\w*' test.txt
Helpful links to read further:
https://www.cyberciti.biz/faq/grep-regular-expressions/
https://regexr.com/

How to grep for files using 'and' operator, words might not be on the same line

I have a directory /dir
which has several text files in it, These files may or may not contain the words 'rock' and 'stone', so basically some files might just contain the word 'rock', some may just contain the word 'stone', some may contain both, and some may contain neither.
How can I list all files in this directory that contain both 'rock' and 'stone'? These words might not be on the same line so I don't think piping through grep twice would work.
Appreciate any help, I was not able to find a stackoverflow post with this problem so I figured I'd ask.
To search files that match the given two (or more) words at any line anywhere in the file, you may want to try ugrep:
ugrep -F --files -e 'rock' --and -e 'stone' dir
This only matches files that have both rock and stone in them. Lines are output that have rock or stone, or you can use option -l to just list files. The -F option searches strings (like grep -F and fgrep), --files applies the --and file-wide, which you want instead of applying the --and per line. Note that we have more than one pattern in this case, so option -e should be used (like grep also requires this).
A shorter form with --bool:
ugrep -F --files --bool 'rock stone' dir
where --bool formulates a Boolean query with space as AND (or use AND).
If you want to search directory dir recursively in subdirectories, use option -r.

Ag / Grep Exact Match Only Search

I am having an issue with using Ag (The Silver Searcher)...
In the docs it says to use -Q for exact match, but I don't understand why it does not work for my purposes. If I type something like ag -Q actions or ag -Q 'actions' into my terminal, it returns all instances of actions, including things like transactions and any other strings that actions is part of.
I have tried a couple other combinations of flags from the docs, including -s and -S, among others, but still I cannot get strictly strings matching just actions to return for me.
I can't get this to work with grep either. Does anyone know how I can get what I need with ag? (or even with grep)...?
Thank you in advance!
Because ag (and grep), find files that contain something. ag -Q means to interpret the search as an exact literal string, not a fuzzy string or a regex. Okay. But a file that has the word "transactions" in it contains exactly, literally the character sequence actions. Sure, it contains more than that too, but that's not surprising.
Probably you're looking for a word-boundary search, grep '\bactions\b' or ag -w -Q actions (maybe ag -w -Q -s actions). But that is not at all the same thing as "just actions", it's a specific requirement on the things surrounding "actions" (namely that they be the beginning or end of a line, or non-letter characters). You have to tell the computer what you actually mean.

How to find match of words with reoccuring character in a file

It might seems like a question that would already have been answered before so pardon me if it's the case, but I can't seems to find a clear answer or an explanation on how to find words in a file with a specified number of repeated character, (ex: words containing 3 times the character '-', such as 'long-and-complex-word').
I'm aware that it is possible to use the command
grep-oE '.{n}'
To find words with consecutive repetition of character, but I'm looking for a way to find repetition of character in no particular order.
Here are the commands that I've tried that aren't working
grep -E '*[-]*[-]*[-]*' file
grep -Ex '* \-* \-* \ -*' file
Thanks.

How to highlight match if grep does not support color option

I'm posting this question here, because I didn't find it elsewhere (SO/Google).
I'm working with "HP-UX 11i Version 3" (at least from man grep) and grep does not support --color option so I was looking for a workaround.
I found some way that works for me (using perl) and I'd say there are other similar (sed, maybe awk and so on), but maybe something completely different.
Answer here on SO - https://unix.stackexchange.com/a/8417/29677 worked well in my environment, I created shell script:
grep $1 | perl -pe 's/'$1'/\e[1;31m$&\e[0m/g'
Works pretty fine, I'm still testing it, one side effect I noticed is, that original grep --color A | grep --color B highlights on Bs, but this solution also highlights As also, but I consider that as a feature.

Resources