grep command on a line from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep command on a line from a file
# 1  
Old 11-12-2011
grep command on a line from a file

Dear forum,

Please excuse my ignorance. I have looked through various forum posts and tried various solutions but have not been able to solve my problem.

File1.txt consist of a list of 100 names (one per line). File2.txt contains details for 1000 people (one per line), with the first field being the name and the other fields being the details

I would like to create a file that has all the lines from the File2.txt that correspond to each of the names from File1.txt.

I have been able to read in File1.txt with a loop and echo out the lines, but unable to perform a grep command successfully.

Here is how the files look like
Code:
#File 1.txt 
jane
peter
mary

#File2.txt
jane brownhair blueeyes
peter blackhair blueeyes
mary brownhair blackeyes

My script looks like this

Code:
while read line
do
grep "$line" File2.txt -n >> tmp.txt
echo $line
done < File1.txt

This printed out all the lines of the file but nothing was found in the tmp.txt file.
I have tried grepping each string eg "grep jane File2.txt" and that was successful, but when grep was used on the variable from the loop, it did not work.

I have also tried removing whitespaces from the files with the commands
Code:
awk '{gsub(/^[ \t]+|[ \t]+$/,"")};1'
awk '{$1=$1};1' bad1 > bad2

but it did not work

I tried doing var1=$(cat File1.txt) and using a for loop on $var1 but it did not work either.

Really appreciate any help.
Thanks a lot.

Beginner0302
Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by vbe; 11-12-2011 at 05:33 PM.. Reason: code tags
# 2  
Old 11-12-2011
I would think this command would give you what you want:

Code:
grep -n -f file1.txt file2.txt

This tells grep that file1 contains a list of patterns, one per line, to use when searching file2. All matches will be written on the standard output.

---------- Post updated at 15:35 ---------- Previous update was at 15:30 ----------

Looking at your script, just for some closure, you need to move the -n option; that will certainly generate error messages though I don't think it should have affected what was matched from file2.

Code:
grep -n "$line"  file2  >>tmp-file


Last edited by agama; 11-12-2011 at 04:38 PM.. Reason: forgot -n -- think you wanted that option
# 3  
Old 11-12-2011
Hi Agama,
Many thanks for your reply.
Unfortunately, grep -f did not work (no output)
When I substituted the actual first line for the variable (with the -n at the back), it returned the correct row (with row number).
I just can't figure out why it does not work with grep -f or with a loop.
Beginner0302
# 4  
Old 11-12-2011
How is formatted your file? ( strictly ONE pattern per line?)
# 5  
Old 11-12-2011
Hi vbe,
Thanks for your reply.
Yes, it is strictly one pattern per line.
I removed whitespace as mentioned with those two awk commands, but am wondering whether it could still be a whitespace problem?
# 6  
Old 11-12-2011
What editor are you using?
# 7  
Old 11-12-2011
emacs or vi
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command on multiple line

Hi All, I have some xml files and I need to find out all xml where one specific type of pattern is available.. Pattern 1 ========== <MAP TIMEOUT="" MODE="STANDALONE"> <COMMENT>without Any Mapping</COMMENT> </MAP> Pattern 2 ========== <MAP TIMEOUT="" MODE="STANDALONE"> ... (4 Replies)
Discussion started by: Baharul
4 Replies

2. Shell Programming and Scripting

Grep command to ignore line starting with hyphen

Hi, I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file. for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'` do . . done How... (4 Replies)
Discussion started by: Srinraj Rao
4 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

grep command and last line

Hello Dear Friends I have a question about grep command when I type the command grep 'energy' mydata.txt it gives me energy= 5001 energy= 8500 energy= 5505 energy= 5506 however I only want to see the last line. How can I use the grep command to pick the last line of grep output. ... (9 Replies)
Discussion started by: rpf
9 Replies

5. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

6. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

7. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

8. UNIX for Dummies Questions & Answers

grep line command

Hi, Can anyone translate in words what this line command does: ls *.fits | grep -v "Zero\|zero" > objlist Obviously ls *.fits means to list all the fits file, grep looks for a certain word, Zero is a certain fits file, and > objlist , created a list with certain items, but I am not sure what... (3 Replies)
Discussion started by: cosmologist
3 Replies

9. Shell Programming and Scripting

Grep using line command

Hello I have a pretty big problem here. I have a very large file having lines running into millions. A small sample of the data is shown below <CRMSUB:MSIN=5000000013,BSNBC=TELEPHON-55108663-TS11&TS21&TS22,MSCAT=ORDINSUB,SUBRES=ALLPLMN-AIRSIMLA,BAOC=OIC,NUMTYP=MULTI;... (3 Replies)
Discussion started by: PradeepRed
3 Replies

10. Shell Programming and Scripting

diffrent results between command line and scripted grep

When I type a command at the command line it supplies one result and the exact same command in a script egrep '^01|^02|^03|^04' file > fileout count = 29353 same count in the script yields a count of 23492 is there any reason this could be happening. (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question