How to print a grep pattern in a new line whilst transferring the pattern to a text file

The Code Ive done so far:

#!/bin/bash

echo "Ramzan: May 13 2021"
echo "Diwali: Nov 04 2021"
echo "Christmas: Dec 25 2021"

mkdir ABCCompany
mkdir ABCCompany/HRDept
mkdir ABCCompany/FinanceDept
touch ABCCompany/HRDept/EmployeeDetails.txt
touch ABCCompany/FinanceDept/SalaryDetails.txt

cd ABCCompany/HRDept


FILE=EmployeeDetails.txt

if [[ -f $FILE ]]; then
     echo "1 Tec101 Sathya permanent Active 1stApril2011" >> EmployeeDetails.txt
     echo "2 Tec102 Laya permanent Active 14thJuly2008" >> EmployeeDetails.txt
     echo "3 Tec103 Hari temporary Active 26thDec2010" >> EmployeeDetails.txt
     echo "4 Tec104 Alex temporary Inactive 26thOct2015" >> EmployeeDetails.txt
     echo "5 Tec105 Lisa temporary Inactive 15thMay2017" >> EmployeeDetails.txt
     echo "6 Tec106 Jisha permanent Active 05thJan2020" >> EmployeeDetails.txt
else
     echo "File Does not Exist"
fi

while read p; do
  grep "Active" $p
done < $FILE

What I need to do:
I need to transfer all Text Lines with the world Active in it from EmployeeDetails to SalaryDetails I have tried every grep and it does not seem to work...

Forgive me im new to this.. Some guidance would be good

Output:

Ramzan: May 13 2021
Diwali: Nov 04 2021
Christmas: Dec 25 2021
grep: 1: No such file or directory
grep: Tec101: No such file or directory
grep: Sathya: No such file or directory
grep: permanent: No such file or directory
grep: Active: No such file or directory
grep: 1stApril2011: No such file or directory
grep: 2: No such file or directory
grep: Tec102: No such file or directory
grep: Laya: No such file or directory
grep: permanent: No such file or directory
grep: Active: No such file or directory
grep: 14thJuly2008: No such file or directory
grep: 3: No such file or directory
grep: Tec103: No such file or directory
grep: Hari: No such file or directory
grep: temporary: No such file or directory
grep: Active: No such file or directory
grep: 26thDec2010: No such file or directory
grep: 4: No such file or directory
grep: Tec104: No such file or directory
grep: Alex: No such file or directory
grep: temporary: No such file or directory
grep: Inactive: No such file or directory
grep: 26thOct2015: No such file or directory
grep: 5: No such file or directory
grep: Tec105: No such file or directory
grep: Lisa: No such file or directory
grep: temporary: No such file or directory
grep: Inactive: No such file or directory
grep: 15thMay2017: No such file or directory
grep: 6: No such file or directory
grep: Tec106: No such file or directory
grep: Jisha: No such file or directory
grep: permanent: No such file or directory
grep: Active: No such file or directory
grep: 05thJan2020: No such file or directory

Hello,

Welcome to the forum ! You've made good progress with this one, but the problem here is that you're not quite using grep as it's intended to be used. The primary function of grep is to search for a given pattern inside a file, or group of files. Those files must either be specified as arguments at the command line, or piped into the grep command as the output of other commands.

For example, let's imagine we have the following file:

# cat fruit.txt
apple
orange
banana
strawberry
#

Let's imagine we've been given the task of finding all lines in this file that contain the string "apple". Now at the moment, the solution you're using is more or less this:

# while read fruit
> do
> grep "apple" "$fruit"
> done < fruit.txt
grep: apple: No such file or directory
grep: orange: No such file or directory
grep: banana: No such file or directory
grep: strawberry: No such file or directory
#

And as you can see, doing it this way, we get the kinds of errors you were reporting.

The reason for this is that whilst we're correctly reading in all the lines of our file (in this case, giving us our fruit names), we're then asking grep to search for our pattern (in my example, "apple") in the contents of the shell variable that we read from each of those lines. Now that's not how grep works - it's meant to look for a pattern inside a file, and not inside a variable, which is what we're asking it to do here, and is the cause of our errors.

If you think about it, for each pass through our loop, the grep command we're trying to run will look like this:

grep apple apple
grep apple orange
grep apple banana
grep apple strawberry

And in every one of those cases, there is no file called "apple", "orange", "banana" or "strawberry". There is a file called "fruit.txt", but we already read that in to our variable $fruit a line at a time on each pass through our loop.

So to do this correctly, you need syntax like this:

# grep apple fruit.txt
apple
# grep strawberry fruit.txt
strawberry
#

So if we bring all this together, you probably want your loop to look something like this instead:

while read p; do
  echo "$p" | grep "Active"
done < "$FILE"

That will have the end result of searching each line of the file for the string "Active", and will display the search string each time it is found, if it is found.

However, it's worth considering that all of this is equivalent to simply typing:

grep Active EmployeeDetails.txt

As it turns out, what you've ended up doing is writing a script to search for a string in every line of a file. Well, that's what grep does on its own, without needing to loop through each line individually. Now if you want to take a particular action based on the contents of a given line, or if there are other things you'll do with the data read from that line if you find the string "Active", then a script makes sense, of course. But if all you want to do is find all the lines of "EmployeeDetails.txt" that contain the string "Active", then the single simple command grep Active EmployeeDetails.txt will do that just fine without you needing to write a script.

Hope this helps ! If you have any further questions, please let us know and we can take things from there.