06-07-2011
grep first occurrence but continue to next entry in patternfile
I have 1300 files (SearchFiles0001.txt, SearchFiles0002.txt, etc.) , each with 650,000 lines, tab-delimited data.
I have a pattern file, with about 1000 lines with a single word. Each single word is found in the 1300 files once.
If I grep -f PatternFile.txt SearchFiles*.txt >OutputFile.txt
the search takes forever, because it searches 650,000 lines x 1300 files for each of the 1000 words.
If I grep -f PatternFile.txt -m 1 SearchFiles*.txt >OutputFile.txt
the search stops after the first line of PatternFile.txt, and I get an OutputFile.txt with 1300 lines: the first entry of PatternFile.txt, but none of the other 999.
How do I make the search faster (by finding the first occurrence of my search parameter, and then moving on to the next search parameter in PatternFile.txt)?
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi, I have a file that looks like this
dasdjasdjoasjdoasjdoa SYN dakspodkapsdka
asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
shfishifhsdifhsidhfif fsdfsdfsdfsdfs
sdfsdfsdfsdsdfsdfsdff cercercercerce
sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
2. UNIX for Dummies Questions & Answers
Hi,
Is there any way of using grep (this may be done in awk, not sure?) that I can stop grep'n a file once I have found the first occurrence of my search string. Looking through grep man pages
-q will exit without printing the lines after the first match, but I need the output.
I have... (5 Replies)
Discussion started by: nhatch
5 Replies
3. UNIX for Dummies Questions & Answers
Hi All,
is there a way to extract the line number of an occurrence using grep?
I know that with the -n option it prints out the line number as well.
I would like to assign the line number to a variable.
Thanks,
Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies
4. UNIX for Dummies Questions & Answers
How would I grep for password file entry without using
grep 'username' /etc/passwd?
perhaps with who?
I want to create alias that will find the password file entry regardless of the user who is using it.
I am trying to get the same exact line from the file entry like:
Name : Password : UserID... (7 Replies)
Discussion started by: alis
7 Replies
5. UNIX Desktop Questions & Answers
How would I grep for password file entry without using
grep 'username' /etc/passwd?
perhaps with who?
I want to create alias that will find the password file entry regardless of the user who is using it.
Thanks (4 Replies)
Discussion started by: alis
4 Replies
6. UNIX Desktop Questions & Answers
hey , i m trying to figure out how to do the following :
i got a text file the looks like so:
1031
1031
1031
1031
1031
1031
1031
1031
16500
16500
16500
16500
1031
1031 (4 Replies)
Discussion started by: boaz733
4 Replies
7. UNIX for Dummies Questions & Answers
Hi,
I'm trying to get lines from a file using identifiers in the first two columns. I have used:
cat MasterFile.txt | grep -f Pattern.txt
and the lines I want display on screen. If I try to put them in a file the file is created but stays empty:
cat MasterFile.txt | grep -f Pattern.txt... (14 Replies)
Discussion started by: FGPonce
14 Replies
8. Shell Programming and Scripting
This one is a bit too challenging for me... Hopefully you guys can help.
Let's say I have a log file called:
"$MW_HOME/user_projects/domains/IDMDomain/servers/wls_ods?/logs/wls_ods1-diagnostic.log"
In this log file I want to search for "DIP-10219". When I execute this
$ cat... (7 Replies)
Discussion started by: exm
7 Replies
9. UNIX for Dummies Questions & Answers
Hello friends, I have a question.
Sometimes I have to search for an entry in a file that is repeated thousands of times. Can you tell me how to search so that i get limited results?
For example:
file: myfile.txt
grep "hello world" myfile.txt
this above grep will generate 5000... (4 Replies)
Discussion started by: DallasT
4 Replies
10. Shell Programming and Scripting
I have file contents
/tmp/x/abc.txt
/home/bin/backup/sys/a.log
I need this output:
/tmp/x/
/home/bin/backup/sys/
Can somebody please help me out
Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: jhonnyrip
3 Replies
GLOB(3) 1 GLOB(3)
glob - Find pathnames matching a pattern
SYNOPSIS
array glob (string $pattern, [int $flags])
DESCRIPTION
The glob(3) function searches for all the pathnames matching $pattern according to the rules used by the libc glob() function, which is
similar to the rules used by common shells.
PARAMETERS
o $pattern
- The pattern. No tilde expansion or parameter substitution is done.
o $flags
- Valid flags:
o GLOB_MARK - Adds a slash to each directory returned
o GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are
sorted alphabetically
o GLOB_NOCHECK - Return the search pattern if no files matching it were found
o GLOB_NOESCAPE - Backslashes do not quote metacharacters
o GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
o GLOB_ONLYDIR - Return only directory entries which match the pattern
o GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.
RETURN VALUES
Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
Note
On some systems it is impossible to distinguish between empty match and an error.
CHANGELOG
+--------+--------------------+
|Version | |
| | |
| | Description |
| | |
+--------+--------------------+
| 5.1.0 | |
| | |
| | |
| | GLOB_ERR was added |
| | |
+--------+--------------------+
EXAMPLES
Example #1
Convenient way how glob(3) can replace opendir(3) and friends.
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "
";
}
?>
The above example will output something similar to:
funclist.txt size 44686
funcsummary.txt size 267625
quickref.txt size 137820
NOTES
Note
This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
Note
This function isn't available on some systems (e.g. old Sun OS).
Note
The GLOB_BRACE flag is not available on some non GNU systems, like Solaris.
SEE ALSO
opendir(3), readdir(3), closedir(3), fnmatch(3).
PHP Documentation Group GLOB(3)