# Grepping a file contents into another file

**URL:** <https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643>\
**Category:** Shell Programming and Scripting\
**Created:** [July 17, 2011, 1:52am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643 "2011-07-17T01:52:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gopikrish81](https://community.unix.com/letter_avatar/gopikrish81/32/5_5575768a8748004e209b776fc1b2916d.png) [@gopikrish81](https://community.unix.com/u/gopikrish81)\
**Post date:** [July 17, 2011, 1:52am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/1 "2011-07-17T01:52:15Z")

</div>

I have a file named as ucid.txt  
It has multiple rows of "id". I need to search and grep each line of it from a file named as pw\_logs.txt and put the results into another file.

Please help ! Thanks.

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [July 17, 2011, 2:10am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/2 "2011-07-17T02:10:21Z")

</div>

Sample input and output please ?

```nohighlight
fgrep

```

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [July 17, 2011, 4:07am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/3 "2011-07-17T04:07:34Z")

</div>

Matrix is right, that's a somewhat confusing specification, but if I have interpreted correctly, this may do what you need:

```nohighlight
cat /dev/null > ucid_output.txt
cat pw_logs.txt | while read searchstring
do
  grep "$searchstring" ucid.txt >> ucid_output.txt
done

```

---

<div class="post-metadata">

**Author:** ![Abhishek\_1984](https://community.unix.com/letter_avatar/abhishek_1984/32/5_5575768a8748004e209b776fc1b2916d.png) [@Abhishek\_1984](https://community.unix.com/u/Abhishek_1984)\
**Post date:** [July 17, 2011, 4:14am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/4 "2011-07-17T04:14:59Z")

</div>

Try this one: (Not tested)

```nohighlight
#!bin/ksh
echo "Enter the id_file_name"
read idfile
echo "Enter the inputfile name"
read infile
echo "Enter the outputfile name"
read outfile

while read line
do
id_1="$line"
grep -i $id_1 $infile>>$outfile
done<$idfile

echo "Completed"

```

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [July 17, 2011, 4:25am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/5 "2011-07-17T04:25:19Z")

</div>

> [@smiling dragon](#):
>
> Matrix is right, that's a somewhat confusing specification, but if I have interpreted correctly, this may do what you need:
> 
> ```plaintext
> cat /dev/null > ucid_output.txt
> cat pw_logs.txt | while read searchstring
> do
> grep "$searchstring" ucid.txt >> ucid_output.txt
> done
> 
> ```

Smiling Dragon - I caught you. Here you go here is an UUOC award to you. 🙂

cat is not needed

```plaintext
while read searchstring
do
  grep "$searchstring" ucid.txt >> ucid_output.txt
done < pw_logs.txt

```

Here is another variant

For every grep output, should not be ideally opening ucid\_output.txt file, write into it and close it, its very expensive operation. Open it once, write contents and close it.

```plaintext
while read searchstring
do
  grep "$searchstring" ucid.txt
done < pw_logs.txt >> ucid_output.txt

```

I generally do not recommend, fgrep as it o(n2) operation, try doing the same thing with hashes.

Please dont take otherwise about the UUOC award comment. Its just for fun!

---------- Post updated at 01:55 PM ---------- Previous update was at 01:53 PM ----------

> [@abhishek\_1984](#):
>
> Try this one: (Not tested)
> 
> #!bin/ksh  
> echo "Enter the id\_file\_name"  
> read idfile  
> echo "Enter the inputfile name"  
> read infile  
> echo "Enter the outputfile name"  
> read outfile
> 
> while read line  
> do  
> id\_1="$line"  
> grep -i $id\_1 $infile\>\>$outfile  
> done\<$idfile
> 
> echo "Completed"

Abhishek,

```plaintext
Please use CODE tags

```

---

<div class="post-metadata">

**Author:** ![tukuyomi](https://community.unix.com/letter_avatar/tukuyomi/32/5_5575768a8748004e209b776fc1b2916d.png) [@tukuyomi](https://community.unix.com/u/tukuyomi)\
**Post date:** [July 17, 2011, 6:06am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/6 "2011-07-17T06:06:35Z")

</div>

Try this one:

```nohighlight
~/unix.com$ grep -Ff ucid.txt pw_logs.txt > another_file.txt

```

---

<div class="post-metadata">

**Author:** ![gopikrish81](https://community.unix.com/letter_avatar/gopikrish81/32/5_5575768a8748004e209b776fc1b2916d.png) [@gopikrish81](https://community.unix.com/u/gopikrish81)\
**Post date:** [July 17, 2011, 9:54am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/7 "2011-07-17T09:54:10Z")

</div>

Sorry for confusion... Here is sample ...

ucid.txt file has following contents...  
abc-123  
abc-124  
bcd-234  
etc...

pw\_logs.txt has following contents..  
12-Jun-11 Log abc-123 Tim xyzfgh  
13-Jun-11 Log abc-123 John pyxrts  
21-Jun-11 Log kjm-345 Tom pgrstg  
24-Jun-11 Log bcd-234 Sam iqstry  
etc...

So I need to grep each line of ucid.txt file from pw\_logs.txt and write to a new file named out.txt which will be having following contents...

12-Jun-11 Log abc-123 Tim xyzfgh  
13-Jun-11 Log abc-123 John pyxrts  
24-Jun-11 Log bcd-234 Sam iqstry

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [July 17, 2011, 8:23pm UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/8 "2011-07-17T20:23:45Z")

</div>

```nohighlight
grep -f ucid.txt pw_logs.txt

```

or

```nohighlight
awk 'NR==FNR {a[$1];next} $3 in a' ucid.txt pw_logs.txt

```

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [July 28, 2011, 10:51am UTC](https://community.unix.com/t/grepping-a-file-contents-into-another-file/291643/9 "2011-07-28T10:51:25Z")

</div>

> [@matrixmadhan](#):
>
> Smiling Dragon - I caught you. Here you go here is an UUOC award to you. 🙂
> 
> cat is not needed

\<grin\> You are most correct! I tend to be a bit lazy with cat in while loops like that. Good catch!
