Grepping a file contents into another file

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.

Sample input and output please ?

fgrep

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

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

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"

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

cat is not needed

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.

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,

Please use CODE tags

Try this one:

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

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.

grep -f ucid.txt pw_logs.txt

or

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

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