"grep -rnw": search for a string in all files - linux

Related question: How do I find all files containing specific text on Linux?
I have been using the command mentioned in the answer of above question to search for string occurences in all files:
grep -rnw '/path/to/somewhere/' -e "pattern"
However lately I encountered a problem, shown in the following picture:
Looks like this command only recognizes strings that stand out as a word or something. How should I modify the command to improve my search result?

explainshell helpfully explains your command, and gives an excerpt from man grep:
-w, --word-regexp
Select only those lines containing matches that form whole words.
So just remove -w since that explicitly does what you don't want:
grep -rn '/path/to/somewhere/' -e "pattern"

Related

Simple grep command to search for a string not working on my ubuntu

I have to search for a string in a file like below using grep which is not working as expected.
It's just a simple search of the string, but not sure why it is not working
echo "Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver" | grep "Lady Necklace"
Can somebody help me here why it's not working, want to know the reason
The command grep will print the whole line matching the pattern.
So
echo "Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver" | grep "Lady Necklace"
will give you
Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver
You might use grep -o or --only-matching to
Print only the matched (non-empty) parts of a matching line, with each
such part on a separate output line.
and to get only
Lady Necklace
Within the comments of the question it was mentioned that a file is used for input. Since the encoding of that is unknown currently, you may also try use character classes
grep -o "Lady[[:space:]]Necklace"
Please see man grep for more options.
You should also have a look into your input file and if the words you like to lookup are in the same line and separated with a space and not with other not printable characters.

GREP - Searching for specific string going backwards

I would like to search file.txt with grep to locate a url ending with. ".doc". When it finds .doc, I want grep to go backwards and find "http://" at the begining of that string.
The output would be http://somesite.com/random-code-that-changes-daily/somefilename.doc
There is only 1 .doc url on this page, so multiple search results should not be an issue.
Please excuse, I am a novice. I did locate the answer at one time but search for 1 hour and can no longer find. I am willing to read and learn but I do not think I'm using the correct search terms for what I want to do. Thank you.
You can use regular expressions,
with the marker ^ you can indicate the start of the line you are looking for.
with the marker $ you can indicate the end of the line you are looking for.
then, you can do something like
grep '^http:\\' \ '.doc$' file.txt
or
grep '^http://\|.doc$' file.txt
or not using regular expressions but just a matching pattern with wildcards as #choroba suggested:
grep 'http://.*\.doc' file.txt
You can also search for http:// and print the line if it contains .doc somewhere after it:
grep 'http://.*\.doc' file.txt
If you want to only print the matching part, use the -o option (if your version of grep supports it).

Using grep to find string in two files?

I would like to find a particular string in two text files - I know how to do it in a single file, but how do I select more than one file, the example below does it for a single text file, I want to search file myfile.txt and otherfile.txt
grep "redeem" /home/tom/myfile.txt
You can do like this,
grep 'redeem' file1 file2 file3..
Syntax:
grep [OPTIONS] PATTERN [FILE...]
Normally, grep can do the pattern matching in more than one files.

from Linux command line, find number of lines in which a string occurs

I have a file in the location /home/someuser/sometext.txt . I want to count the number of lines in which a particular string occurs. What's the way to do that from Linux command line?
grep with -c switch is what you need:
grep -c "pattern" /home/someuser/sometext.txt
Alternate solution using awk:
awk '/regex/{c++}END{print c+0}' /home/someuser/sometext.txt
You're looking for the grep command. Here's a basic tutorial. It's extremely useful for string searching in files. It also has support for regular expressions.
It looks like you'll do something like this:
grep -c "mystring" /home/someuser/sometext.txt
The -c argument is short for --count and tells grep to print out the number of lines that contain the string.

Terminal command to find lines containing a specific word?

I was just wondering what command i need to put into the terminal to read a text file, eliminate all lines that do not contain a certain keyword, and then print those lines onto a new file. for example, the keyword is "system". I want to be able to print all lines that contain system onto a new separate file. Thanks
grep is your friend.
For example, you can do:
grep system <filename> > systemlines.out
man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)
If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.
grep '\<system\>'
Will search for lines that contain the word system, and not system as a substring.
below grep command will solve ur problem
grep -i yourword filename1 > filename2
with -i for case insensitiveness
without -i for case sensitiveness
to learn how grep works on ur server ,refer to man page on ur server by the following command
man grep
grep "system" filename > new-filename
You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

Resources