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.
Related
Tried the following command, it lists all the lines including file names
which are not matching the given pattern.
grep -nrv "^type.* = .*"
"But what we need is list of file names in a folder with content
which does not have even a single occurrence of above pattern."
Your help will be really appreciated.
You need the -L option:
grep -rL '^type.* = .*' directory_name
From the GNU grep manual:
-L, - -files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
I'm trying to remove a long text of various lines contained in a file from another file.
I've tried with
egrep -v $(cat text) original
with no success....
You can use -f pattern-file option in grep:
grep -vxFf test original
x is for exact pattern match (remove this if you want partial match also)
F is for fixed string search instead of regex
If I have two strings, for example "class" and "btn", what is the linux command that would allow me to search for these two strings in the entire directory.
To be more specific, lets say I have directory that contains few folders with bunch of .php files. My goal is to be able to search throughout those .php files so that it prints out only files that contain "class" and "btn" in one line. Hopefully this clarifies things better.
Thanks,
I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.
grep -rn "text_to_search" directory_name/
example:
$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string
output would be:
menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);
grep -r /path/to/directory 'class|btn'
grep is used to search a string in a file. With the -r flag, it searches recursively all files in a directory.
Or, alternatively using the find command to "identify" the files to be searched instead of using grep in recursive mode:
find /path/to/your/directory -type f -exec grep "text_to_search" {} \+;
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"
I have 70+ strings I need to find and delete in a file. I need to remove the entire line in the file that the string appears in.
I know I can use sed -i '/string to remove/d' fileA.txt to remove them one at a time. However, considering I have 70+, it will take some time doing it this way.
Is there a way I can put these 70+ strings in a file and have sed go through them one by one? Or if I create a file containing the strings, is there a way to compare the two files so it removes any line from fileA that contains one of the strings?
You could use grep:
grep -vf file_with_words.txt file.txt
where file_with_words.txt would be the file containing the list of words, each word being on a different line and file.txt is the file that you want to remove the lines from.
If your list of words contains regex metacharacters, then tell grep to consider those as fixed strings (if that is what you want):
grep -F -vf file_with_words.txt file.txt
Using sed, you'd need to say:
sed '/word1\|word2\|word3/d' file.txt
or
sed -E '/word1|word2|word3/d' file.txt
You could use command substitution to construct the pattern too:
sed -E "/$(paste -sd'|' file_with_words.txt)/d" file.txt
but grep is clearly the tool to use in this case.
If you want to do the job in bash, here's how:
search=fileA.txt
queries=queries.txt
while read query
do
sed -i '' "/$query/d" $search
done < "$queries"
where queries.txt looks like
I
want
to
delete
these
lines