grep Everything after a string - grep

I need to only grep the md5 hash
this is the hash
MD5 (mt.pm) = adcddd9492c707642d2bcffbfc67b7a6
it needs to look like this
adcddd9492c707642d2bcffbfc67b7a6
or to do the reverse
crapb0c63a3cb776502fe03706b2fd540439 /home/mta.pm"
and only get the hash
now clue how to
any Help

To grep, do the following (this will not work in all grep implementations):
grep -o '[a-z0-9]*$'
or you can use sed:
sed 's/.*= *\([a-z0-9]*\)$/\1/'

Try this (GNU grep):
grep -oP '.* \K.*$'
Or better :
grep -o '[[:xdigit:]]\{32\}$'
Or with bash :
read -a arr <<< 'MD5 (mt.pm) = adcddd9492c707642d2bcffbfc67b7a6'
echo ${arr[-1]}
With \{32\} it's much stronger. md5 is always 32 hexadecimal characters, see http://en.wikipedia.org/wiki/MD5
[[:xdigit:]] is a POSIX class regex, that means to match only hex chars.
FINALLY
If you want to match a 32 hex characters long in a string :
grep -o '[[:xdigit:]]\{32\}'
will do the trick.

Related

How to not conflict with filename when combining grep commands?

Imagine you want to grep recursively for string1 but not string1_suffix. Trivial approach would be
grep -r string1 | grep -v string1_suffix`
But what if the file names can contain string1_suffix?
A line containing string1_suffix_data.json: blabla string1 would be filtered away by the second grep.
Is it possible to circumvent this somehow? Of course in this trivial example I could just turn around the first and the second part, but what about the general case?
If you have PCRE with -P option, you can use string1(?!_suffix)
For a general case, use ^(?!.*str2).*str1 to match lines containing str1 but not str2
With find+awk (tested on GNU awk, not sure about other implementations)
find -type f -exec awk '/str1/ && !/str2/{print FILENAME ":" $0}' {} +

Grep Tab, Carriage Return, & New Line

I'm trying to use Grep to find a string with Tabs, Carriage Returns, & New Lines. Any other method would be helpful also.
grep -R "\x0A\x0D\x09<p><b>Site Info</b></p>\x0A\x0D\x09<blockquote>\x0A\x0D\x09\x09<p>\x0A\x0D\x09</blockquote>\x0A\x0D</blockquote>\x0A\x0D<blockquote>\x0A\x0D\x09<p><b>More Site Info</b></p>" *
From this answer
If using GNU grep, you can use the Perl-style regexp:
$ grep -P '\t' *
Also from here
Use Ctrl+V, Ctrl+M to enter a literal Carriage Return character into your grep string. So:
grep -IUr --color "^M"
will work - if the ^M there is a literal CR that you input as I suggested.
If you want the list of files, you want to add the -l option as well.
Quoting this answer:
Grep is not sufficient for this operation.
pcregrep, which is
found in most of the modern Linux systems can be used ...
Bash Example
$ pcregrep -M "try:\n fro.*\n.*except" file.py
returns
try:
from tifffile import imwrite
except (ModuleNotFoundError, ImportError):

How to match a non string in gnu grep

I'll use an example to illustrate my problem. Suppose we have the file name 'file.txt' that contains the following string:
AooYoZooYZoAoooooYZ
I'd like to use grep to find all substrings that begin with 'A' and end with 'YZ' but do not contain 'YZ' in between the 'A' and 'YZ'. The desired output would be:
AooYoZooYZ
AoooooYZ
My best guess is to do the following:
$grep -E -o 'A[^(YZ)]*YZ' file.txt
But the output is only:
AoooooYZ
I'd like the parentheses to hold their meaning for the YZ but I read in the GNU grep manual (http://www.gnu.org/software/grep/manual/grep.html) that:
"Most meta-characters lose their special meaning inside bracket expressions." I've also tried:
$grep -E -o 'A.*YZ file.txt
But this outputs the entire line:
AooYoZooYZoAoooooYZ
Is there a way to override this or another way of solving my problem?
Maybe you can use non-greedy match which can be used in Perl regexp
echo 'AooYoZooYZoAoooooYZ' | grep -P -o 'A.*?YZ'
However, note that the manual for GNU grep says that -P option is highly experimental.

Getting numbers from a string with grep

I came with another simple question...
I got a string with a substring in the format xx:xx:xx where the x's are numbers. I want to extract that substring including the ":" symbol, so my output would be "xx:xx:xx".
I think it can be done with a grep -Eo [0-9], but im not sure of the syntax... Any help?
echo "substring in the format 12:43:37 where the x's are numbers" |
grep -o '[0-9:]*'
Output:
12:43:37
If you have other numbers in the input string you can be more specific:
grep -o '[0-9]*:[0-9]*:[0-9]*'
even:
grep -o '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'

How to escape parenthesis in grep

I want to grep for a function call 'init()' in all JavaScript files in a directory. How do I do this using grep?
Particularly, how do I escape parenthesis, ()?
It depends. If you use regular grep, you don't escape:
echo '(foo)' | grep '(fo*)'
You actually have to escape if you want to use the parentheses as grouping.
If you use extended regular expressions, you do escape:
echo '(foo)' | grep -E '\(fo*\)'
If you want to search for exactly the string "init()" then use fgrep "init()" or grep -F "init()".
Both of these will do fixed string matching, i.e. will treat the pattern as a plain string to search for and not as a regex. I believe it is also faster than doing a regex search.
$ echo "init()" | grep -Erin 'init\([^)]*\)'
1:init()
$ echo "init(test)" | grep -Erin 'init\([^)]*\)'
1:init(test)
$ echo "initwhat" | grep -Erin 'init\([^)]*\)'
Move to your root directory (if you are aware where the JavaScript files are). Then do the following.
grep 'init()' *.js

Resources