Grepping a file contents into another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping a file contents into another file
# 1  
Old 07-17-2011
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.
# 2  
Old 07-17-2011
Sample input and output please ?
Code:
fgrep

# 3  
Old 07-17-2011
Matrix is right, that's a somewhat confusing specification, but if I have interpreted correctly, this may do what you need:
Code:
cat /dev/null > ucid_output.txt
cat pw_logs.txt | while read searchstring
do
  grep "$searchstring" ucid.txt >> ucid_output.txt
done

# 4  
Old 07-17-2011
Try this one: (Not tested)
Code:
#!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"


Last edited by Franklin52; 07-17-2011 at 07:31 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 07-17-2011
Quote:
Originally Posted by Smiling Dragon
Matrix is right, that's a somewhat confusing specification, but if I have interpreted correctly, this may do what you need:
Code:
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. Smilie

cat is not needed

Code:
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.

Code:
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 ----------

Quote:
Originally Posted by 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,

Code:
Please use CODE tags

# 6  
Old 07-17-2011
Try this one:
Code:
~/unix.com$ grep -Ff ucid.txt pw_logs.txt > another_file.txt

# 7  
Old 07-17-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

7. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

8. Shell Programming and Scripting

Grepping from a point in a file to the end of the file

does any one know how to turn the equivalent of this command: awk '/2011 John Doe 8344/,0' /tmp/ops.log | egrep -c "received request" to something that would use egrep instead of awk? What the awk command does is, it searches the ops.log file for "2011 John Doe 8344". When it finds it,... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

10. Shell Programming and Scripting

Grepping a file based on input from a second file

how to grep a file based on another input file File1 ashu 1 ninetwo hari qwer 6 givefour jan fghj 8 noeight mar vbmi 7 noput feb -- --- File2 noput noeight --- -- Taking the input of grep as File2, a search need to be made in File1 giving File3 as output: (7 Replies)
Discussion started by: er_ashu
7 Replies
Login or Register to Ask a Question