-1

I wrote a small script that find a line containing specific string I provide to the bash and write them to a text file.

 grep -A $2 "$1" outFile.txt >> "$1-results.txt"

Now I want the search to be applied to 1000 file but the results will be in one text file.

The second thing I want is before starting the search of every file to write add a heading ' FILE 1 ' and when when File 1 finishes I add 'FILE 2' etc.

I have found answers but they did not work with me in a file , the worked on the terminal only. I tried to make some modification but I faild.

Update:

Directory has the following files:

res1.txt ........ res1000.txt 

Input as an example:

Jhon Age 

Which mean get any person with the name jhon and bring his age with the name.

Expected out put :

      FILE1
 Jhon      43
 Jhon      55 
      FILE2
  Jhon     66
      .
      .
      .
      .
      .
     FILE3
      .
      .
      .

etc..

0xMe
  • 49
  • 6
  • Please post sample input files and expected output samples in code tags into your post please. – RavinderSingh13 Oct 29 '17 at 11:46
  • `grep -H` is better than adding headers: it'll prepend every search result with file name, no need to look it up, easy filtering/counting and such. – ivan_pozdeev Oct 29 '17 at 11:56
  • https://unix.stackexchange.com/questions/171314/how-to-grep-for-same-string-but-multiple-files-at-the-same-time – ivan_pozdeev Oct 29 '17 at 12:01
  • What's wrong with `grep -A "$2" "$1" *.txt >> results.txt`? – randomir Oct 29 '17 at 12:04
  • it says no such file or directory *.txt. And I could not format the results as I want in the text file. Or probably I don't know how Got my point? – 0xMe Oct 29 '17 at 13:11
  • @ivan_pozdeev Not a possible duplicate, very different request. – 0xMe Oct 29 '17 at 13:13
  • @ushehri, it's not clear from your question where those 1000 file(names) come from? If they are all under one directory, you can use `grep -R` to grep recursively. The `*.txt` was only an example if you wanted to grep all txt files. So, what is your input, and what is the expected output? – randomir Oct 29 '17 at 15:59
  • Update Question – 0xMe Oct 29 '17 at 18:14
  • So, what specifically is it you're stuck with? [Just asking for code for your specific case is off topic 'cuz it's not reusable.](https://meta.stackoverflow.com/a/338499/648265) – ivan_pozdeev Oct 30 '17 at 01:25
  • The duplicate tells you how to get the file name output. Refactoring it to produce the file name on a separate output line is a simple matter of rewriting the output. – tripleee Oct 30 '17 at 08:19
  • @tripleee, I want to write my self "FILE1" or any other string as as I like . so please it not even close to the duplicate. . – 0xMe Oct 30 '17 at 20:54
  • I added a second duplicate which addresses that part. – tripleee Oct 31 '17 at 04:38
  • @tripleee You just in love with duplicates – 0xMe Oct 31 '17 at 17:24
  • Is there still a problem or did you manage to solve your task with these links? – tripleee Nov 01 '17 at 08:13
  • I did solve it by using java as helper class – 0xMe Nov 01 '17 at 17:22

1 Answers1

-1

I think it's what you look for

for i in *.txt;do
  result=$(grep -A "$2" "$1" "$i")
  [ $? -eq 0 ] && { echo "$i"; echo "$result"; }
done
ctac_
  • 2,413
  • 2
  • 7
  • 17