I have a string (or a list of strings) that is/are to be searched in all the files of a directory. For example, if I am searching for ABC, and file1.txt contains ABC on line 6, then I want lines 6, 7, 8, and 9 from that file.
I want all such output to go to a new output file.
Asked
Active
Viewed 1,168 times
0
G-Man Says 'Reinstate Monica'
- 22,130
- 27
- 68
- 117
Praveen Kashekar
- 7
- 2
-
Please expand the question to include an example. – steve Jul 19 '15 at 16:55
-
For E.G: string=ABC, directory=DIR1(contains huge text files). i want to search 'ABC' in all the files of directory 'DIR1'. suppose if we have ABC string in 6th line of file1.txt means i need 6,7,8,9 to be copied to output file. This is simple example. but i have big list to be searched. – Praveen Kashekar Jul 19 '15 at 16:59
3 Answers
3
Perhaps this suits. Makes use of the grep -A (after-context) option, to output the matching line and the 3 subsequent lines.
find . -name file\* -exec grep -A 3 ABC {} \; > result-filename
NOTE: You shouldn't place result-filename in directoryname. (See: grep: input file 'X' is also the output).
To cover multiple patterns:
find . -name file\* -exec egrep -A 3 'ABC|XYZ' {} \; > result-filename
Or multiple patterns from a file (thanks Evgeny Vereshchagin) :
find . -name file\* -exec grep -A 3 -f patterns.txt {} \; > result-filename
-
Instead of single string, If i have list of strings. how can i perform ? – Praveen Kashekar Jul 19 '15 at 17:38
-
put your strings in file `patterns` (one string per line) and run `find dir -type f -exec grep -A 3 -f patterns {} \; >result` – Evgeny Vereshchagin Jul 19 '15 at 18:00
-
(1) +1 for `grep -A`. (2) Why are you suggesting **`-name file\*`** as part of your answer? The filename `file1.txt` was just an example; the OP never said that he’s looking for files *whose names begin with `file`*. Perhaps you meant `-type f`? (3) Why not just say `grep -A 3 -f patterns.txt *`? The question is imprecise regarding the meaning of “all the files of a directory”. If you need to include “hidden” files, use `shopt -s dotglob`. If you need to recurse into subdirectories, say `shopt -s globstar` and use `grep … **`. – G-Man Says 'Reinstate Monica' Jul 19 '15 at 21:42
-
(3) Didn't like the `grep -A 3 -f patterns.txt *` as it would prefix each line with the filename and also include an occasional `--` – steve Jul 19 '15 at 21:55
1
The following will search all not-dot files in the current directory:
sed '/string/{
$!N;$!N;$!N;$!N
w ./new_out_file
}' ./*
To do multiple -Fixed-string matches with grep -A w/o find and w/o the filenames prepended to each match, you just first need a single stream.
cat ./* | grep -A2 -Ff pattern_file >outfile
mikeserv
- 57,448
- 9
- 113
- 229
-
Thanks ... but if i have list of names to searched in files of a directory and need same 4 lines from its occurrence to copied to output. How it gonna help ?? – Praveen Kashekar Jul 19 '15 at 16:41
-
1@PraveenKashekar I don't know what this means. This is an answer to the question you asked. – mikeserv Jul 19 '15 at 16:50
-1
I got solution for it:
grep -A 2 -f 'INPUT.txt' *.* >> RESULT.txt
chaos
- 47,463
- 11
- 118
- 144
Praveen Kashekar
- 7
- 2
-
2That prepends the filename in which each match is found to the matched lines. That's nothing like what is requested in your question. – mikeserv Jul 21 '15 at 17:29