Searching strings on files Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionSearching for string in filesCommand-line tool to search docx filesSafely convert unicode strings to printable characterssearching multiple strings in multiple files inside a directory and printing the string and corresponding file name where it was foundStrings not outputting text found using grepCompare columns between different filesWhy grep shows different results when I use file1 as a pattern on file2 and viceversa?How to list all files in a directory with absolute pathsRunning multiple instances of perl via xargsRegEx for matching strings in 2nd and 5th columns using grep
Check which numbers satisfy the condition [A*B*C = A! + B! + C!]
Bete Noir -- no dairy
What is the meaning of the new sigil in Game of Thrones Season 8 intro?
Why didn't this character "real die" when they blew their stack out in Altered Carbon?
Can an alien society believe that their star system is the universe?
What is a non-alternating simple group with big order, but relatively few conjugacy classes?
How to bypass password on Windows XP account?
What's the purpose of writing one's academic biography in the third person?
Is the Standard Deduction better than Itemized when both are the same amount?
How does the particle を relate to the verb 行く in the structure「A を + B に行く」?
When do you get frequent flier miles - when you buy, or when you fly?
Why aren't air breathing engines used as small first stages
Why is "Consequences inflicted." not a sentence?
Why do we bend a book to keep it straight?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
How do I stop a creek from eroding my steep embankment?
List *all* the tuples!
How do pianists reach extremely loud dynamics?
Using et al. for a last / senior author rather than for a first author
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Is it true that "carbohydrates are of no use for the basal metabolic need"?
How widely used is the term Treppenwitz? Is it something that most Germans know?
Why was the term "discrete" used in discrete logarithm?
Why do people hide their license plates in the EU?
Searching strings on files
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionSearching for string in filesCommand-line tool to search docx filesSafely convert unicode strings to printable characterssearching multiple strings in multiple files inside a directory and printing the string and corresponding file name where it was foundStrings not outputting text found using grepCompare columns between different filesWhy grep shows different results when I use file1 as a pattern on file2 and viceversa?How to list all files in a directory with absolute pathsRunning multiple instances of perl via xargsRegEx for matching strings in 2nd and 5th columns using grep
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've got a bunch on strings which I need to find in a couple of files, for example:
string1
string2
stringn
file1.txt
file2.txt
filen.txt
Is there an (easy) way to do that in bash? I need to know, if a string was found, in which file is it.
command-line search
migrated from stackoverflow.com Apr 28 '11 at 16:25
This question came from our site for professional and enthusiast programmers.
add a comment |
I've got a bunch on strings which I need to find in a couple of files, for example:
string1
string2
stringn
file1.txt
file2.txt
filen.txt
Is there an (easy) way to do that in bash? I need to know, if a string was found, in which file is it.
command-line search
migrated from stackoverflow.com Apr 28 '11 at 16:25
This question came from our site for professional and enthusiast programmers.
add a comment |
I've got a bunch on strings which I need to find in a couple of files, for example:
string1
string2
stringn
file1.txt
file2.txt
filen.txt
Is there an (easy) way to do that in bash? I need to know, if a string was found, in which file is it.
command-line search
I've got a bunch on strings which I need to find in a couple of files, for example:
string1
string2
stringn
file1.txt
file2.txt
filen.txt
Is there an (easy) way to do that in bash? I need to know, if a string was found, in which file is it.
command-line search
command-line search
edited Apr 28 '11 at 19:16
Gilles
548k13011131631
548k13011131631
asked Apr 28 '11 at 15:09
MauJFernandezMauJFernandez
132
132
migrated from stackoverflow.com Apr 28 '11 at 16:25
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Apr 28 '11 at 16:25
This question came from our site for professional and enthusiast programmers.
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Simple grep command with -e option:
grep -e "string1" -e "string2" -e "stringn" file*.txt
Or you can put all the search strings in a file called search.txt like this:
string1
string2
string3
...
...
stringN
and then run grep like this with -f
option:
grep -f search.txt file*.txt
you can use stdin as fileecho -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
add a comment |
Use grep to search for all the strings in one pass:
grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt
The -E
lets you use the pipe character(|) without escaping it. The -H
prints the filename for each match. The regular expression uses pipes to separate each string, so that grep
will try to match each one in order.
add a comment |
There is a variant of grep that supports this feature for large sets of strings, try
fileWithListOfSearchTargets=myFileOfSearchTargets.txt
fgrep -f $fileWithListOfSearchTargets file1 file2 ... filen
(The variable and filenames are meant to be self-documenting, you can use any name you like)
You have to enter all your search strings into the file.
No leading or trailing spaces unless you expect those to match in your filelist.
There is a limit to the size that most fgrep
s can process. Don't try to cram 10K lines into one file.
add a comment |
The best way is to use grep:
grep -H 'string to search' file1.txt file2.txt filen.txt
will search the specified files for a string, and print out the matching lines along with the filename where the match was found.
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.
– Mike R
Apr 28 '11 at 15:13
add a comment |
Unix find command:
find . -exec grep "i want to find this string" '' ; -print
will search from current dir and down.
This works too:
egrep -r 'arbitrary string' *
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
add a comment |
egrep "(string1|string2|string3)" file1..3.txt
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f12219%2fsearching-strings-on-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple grep command with -e option:
grep -e "string1" -e "string2" -e "stringn" file*.txt
Or you can put all the search strings in a file called search.txt like this:
string1
string2
string3
...
...
stringN
and then run grep like this with -f
option:
grep -f search.txt file*.txt
you can use stdin as fileecho -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
add a comment |
Simple grep command with -e option:
grep -e "string1" -e "string2" -e "stringn" file*.txt
Or you can put all the search strings in a file called search.txt like this:
string1
string2
string3
...
...
stringN
and then run grep like this with -f
option:
grep -f search.txt file*.txt
you can use stdin as fileecho -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
add a comment |
Simple grep command with -e option:
grep -e "string1" -e "string2" -e "stringn" file*.txt
Or you can put all the search strings in a file called search.txt like this:
string1
string2
string3
...
...
stringN
and then run grep like this with -f
option:
grep -f search.txt file*.txt
Simple grep command with -e option:
grep -e "string1" -e "string2" -e "stringn" file*.txt
Or you can put all the search strings in a file called search.txt like this:
string1
string2
string3
...
...
stringN
and then run grep like this with -f
option:
grep -f search.txt file*.txt
answered Apr 28 '11 at 15:17
anubhavaanubhava
37146
37146
you can use stdin as fileecho -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
add a comment |
you can use stdin as fileecho -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
you can use stdin as file
echo -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
you can use stdin as file
echo -e "string1nstring2nstringn" | grep -f - search.txt file*.txt
– jcubic
Apr 28 '11 at 15:24
add a comment |
Use grep to search for all the strings in one pass:
grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt
The -E
lets you use the pipe character(|) without escaping it. The -H
prints the filename for each match. The regular expression uses pipes to separate each string, so that grep
will try to match each one in order.
add a comment |
Use grep to search for all the strings in one pass:
grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt
The -E
lets you use the pipe character(|) without escaping it. The -H
prints the filename for each match. The regular expression uses pipes to separate each string, so that grep
will try to match each one in order.
add a comment |
Use grep to search for all the strings in one pass:
grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt
The -E
lets you use the pipe character(|) without escaping it. The -H
prints the filename for each match. The regular expression uses pipes to separate each string, so that grep
will try to match each one in order.
Use grep to search for all the strings in one pass:
grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt
The -E
lets you use the pipe character(|) without escaping it. The -H
prints the filename for each match. The regular expression uses pipes to separate each string, so that grep
will try to match each one in order.
answered Apr 28 '11 at 15:19
Brigham
add a comment |
add a comment |
There is a variant of grep that supports this feature for large sets of strings, try
fileWithListOfSearchTargets=myFileOfSearchTargets.txt
fgrep -f $fileWithListOfSearchTargets file1 file2 ... filen
(The variable and filenames are meant to be self-documenting, you can use any name you like)
You have to enter all your search strings into the file.
No leading or trailing spaces unless you expect those to match in your filelist.
There is a limit to the size that most fgrep
s can process. Don't try to cram 10K lines into one file.
add a comment |
There is a variant of grep that supports this feature for large sets of strings, try
fileWithListOfSearchTargets=myFileOfSearchTargets.txt
fgrep -f $fileWithListOfSearchTargets file1 file2 ... filen
(The variable and filenames are meant to be self-documenting, you can use any name you like)
You have to enter all your search strings into the file.
No leading or trailing spaces unless you expect those to match in your filelist.
There is a limit to the size that most fgrep
s can process. Don't try to cram 10K lines into one file.
add a comment |
There is a variant of grep that supports this feature for large sets of strings, try
fileWithListOfSearchTargets=myFileOfSearchTargets.txt
fgrep -f $fileWithListOfSearchTargets file1 file2 ... filen
(The variable and filenames are meant to be self-documenting, you can use any name you like)
You have to enter all your search strings into the file.
No leading or trailing spaces unless you expect those to match in your filelist.
There is a limit to the size that most fgrep
s can process. Don't try to cram 10K lines into one file.
There is a variant of grep that supports this feature for large sets of strings, try
fileWithListOfSearchTargets=myFileOfSearchTargets.txt
fgrep -f $fileWithListOfSearchTargets file1 file2 ... filen
(The variable and filenames are meant to be self-documenting, you can use any name you like)
You have to enter all your search strings into the file.
No leading or trailing spaces unless you expect those to match in your filelist.
There is a limit to the size that most fgrep
s can process. Don't try to cram 10K lines into one file.
edited 10 hours ago
Rui F Ribeiro
42.1k1484142
42.1k1484142
answered Apr 28 '11 at 15:24
shelltershellter
53238
53238
add a comment |
add a comment |
The best way is to use grep:
grep -H 'string to search' file1.txt file2.txt filen.txt
will search the specified files for a string, and print out the matching lines along with the filename where the match was found.
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.
– Mike R
Apr 28 '11 at 15:13
add a comment |
The best way is to use grep:
grep -H 'string to search' file1.txt file2.txt filen.txt
will search the specified files for a string, and print out the matching lines along with the filename where the match was found.
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.
– Mike R
Apr 28 '11 at 15:13
add a comment |
The best way is to use grep:
grep -H 'string to search' file1.txt file2.txt filen.txt
will search the specified files for a string, and print out the matching lines along with the filename where the match was found.
The best way is to use grep:
grep -H 'string to search' file1.txt file2.txt filen.txt
will search the specified files for a string, and print out the matching lines along with the filename where the match was found.
answered Apr 28 '11 at 15:12
Marc B
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.
– Mike R
Apr 28 '11 at 15:13
add a comment |
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.
– Mike R
Apr 28 '11 at 15:13
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:
for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.– Mike R
Apr 28 '11 at 15:13
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example:
for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done
However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping.– Mike R
Apr 28 '11 at 15:13
add a comment |
Unix find command:
find . -exec grep "i want to find this string" '' ; -print
will search from current dir and down.
This works too:
egrep -r 'arbitrary string' *
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
add a comment |
Unix find command:
find . -exec grep "i want to find this string" '' ; -print
will search from current dir and down.
This works too:
egrep -r 'arbitrary string' *
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
add a comment |
Unix find command:
find . -exec grep "i want to find this string" '' ; -print
will search from current dir and down.
This works too:
egrep -r 'arbitrary string' *
Unix find command:
find . -exec grep "i want to find this string" '' ; -print
will search from current dir and down.
This works too:
egrep -r 'arbitrary string' *
answered Apr 28 '11 at 15:12
eggie5
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
add a comment |
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess.
– Caleb
May 7 '11 at 11:15
add a comment |
egrep "(string1|string2|string3)" file1..3.txt
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
add a comment |
egrep "(string1|string2|string3)" file1..3.txt
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
add a comment |
egrep "(string1|string2|string3)" file1..3.txt
egrep "(string1|string2|string3)" file1..3.txt
edited Apr 29 '11 at 3:43
answered Apr 28 '11 at 18:03
user unknownuser unknown
7,47112450
7,47112450
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
add a comment |
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead.
– Caleb
May 7 '11 at 11:14
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta.
– user unknown
May 7 '11 at 12:55
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f12219%2fsearching-strings-on-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-command-line, search