Display X characters after matched word - grep

I'd like to display the file and version number for different services. For example there might be a line in the text file that says nginx 1.13.0. I'd like to be able to search every single instance of nginx [0-9].[0-9].[0-9] and have it displayed with the version number and file name / line.
I've already tried this command which works well for displaying the matched files:
grep -lrEH "nginx [0-9].[0-9].[0-9]"

You are close, missing that it might be more than one digit +. You should also escape the ., so it mean only . and not any character.
Try:
grep -EHro "nginx [0-9]+\.[0-9]+\.[0-9]+"

Related

Removing docker Image tag id from image names

I have list of docker image names like below
aa-bb-cc-2.10.0-14
aa-bc-cd-ef-ghi-2.10.0-410
I want this to be replaced as
aa-bb-cc:2.10.0-14
aa-bc-cd-ef-ghi:2.10.0-410
There are a lot of such docker images names where I want to remove the docker image tag from the complete name. How can I replace all at a single go. Any script or formatting in Notepad++. Can anyone help with this.
Thanks.
Ctrl+H
Find what: (?<=[a-z])-(?=\d)
Replace with: :
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
(?<=[a-z]) # positive lookbehind, make sure we have a letter before
- # hyphen
(?=\d) # positive lookahead, make sure we have a digit after
Screenshot (before):
Screenshot (after):

avoid following dash in grep results

I am trying to select the line that has the name "paul" in it.
!grep -w '^paul' some_file
This also returns the lines starting with paul-henri. How do I select the single line that starts with the word 'paul' only?
(In other words, dash - or slash / and dot . are getting selected if followed by the word paul)
Update:
Thanks to Tim, this worked:
grep -w '^paul' some_file | grep -vE 'paul[-./?]'
You could match on the pattern ^paul[^-]:
!grep -w '^paul[^-]' some_file
This would match any line starting with paul, which is then followed by one or more characters other than dash. If you need to also match possible lines starting with and containing only paul, then you might need to use a negative lookahead:
^paul(?!-)
But, this would require an extended version of grep, and your version of grep might not support it.

Linux word search using grep

I want to search a file in which there any words which contain alphanumeric words (i.e. words that have both combination of alpha and numeral)
I have tried using different grep combinations but not able to find the exact result I want to achieve
for example if I have a file that contains multiple lines
asbcd acblk54 lkasdfn
098213 102938 091283
aalk adsf adf
lkjas 0098324 0980 assdf
alkj30lkl 093adflkj 0lkdsf094
since lines 1 and 5 contain words which are alphanumeric only two lines should be filtered. how can I achieve this using grep.(line 2 contains numerals only, line 3 contains alpha only, line 4 contains words that are either alpha or numeral but not combination of both)
What you are interested in is a grep that matches full words. So you need the -w option:
-w, --word-regexp: Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. This option has no effect if -x is also specified.
source: man grep
The regex you search for uses indeed [[:alnum:]] but you have to ensure that it has both a [[:alpha:]] and a [[:digit:]]. A word containing both must thus have a sequence [[:alpha:]][[:digit:]] or [[:digit:]][[:alpha:]]. The regex you are after is thus: [[:alnum:]]*([[:alpha:]][[:digit:]]|[[:digit:]][[:alpha:]])[[:alnum:]]*
The following grep will do the matches:
$ grep -w -E '[[:alnum:]]*([[:alpha:]][[:digit:]]|[[:digit:]][[:alpha:]])[[:alnum:]]*' file

Word search using grep command in Linux

Suppose I have a file named as test.txt having content .
I want to find the line containing the words starting with "r" character and ending with "i" character?
That would be something like:
grep '\b[Rr][A-Za-z]*[Ii]\b' test.txt
That's case insensitive so, if you want to ensure specific capitalisation, you would adjust the individual character classes in the expression.

can grep identify only one matching word in a file?

I have a file with a list of word and I want to identify only the word in the file which exactly matches another word?
So, for example, if I have in the file, the words "BEBE, BEBÉ, BEBÉS", and I look for "BEBE", I want it to return just the first one, which is the exact match.
I tried using grep -w "BEBE" filename.txt, but it doesn't work. It still gives me back all three of them.
Use -o to only display the part that matches with -w, also use -F for fixed string if you're not regex matching:
$ cat file
BEBE, BEBÉ, BEBÉS
$ grep -woF 'BEBÉ' file
BEBÉ
$ grep -woF 'BEBÉS' file
BEBÉS

Resources