For Loop Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop Question
# 1  
Old 03-13-2012
For Loop Question

I am struggling with the for loop. I have a file name heros.txt and I would like to go through a list in file where.txt and see if I can find the name from where inside heros.

One of the problems that I am having is I dont understand how to setup the for loop to find the list to search.Smilie

Then after that be able to print out the name and reason for why they did not show up.

I have been playing with grep but it is taking the names separated by space. I will search for Parker, then Peter not just Parker, Peter.

Any help would be appreciated


Files:
heros.txt
Code:
America, Captain 54329876 09 A A Sick
Rogers, Steve 542986 09 A A NOT HERE
Man, Spider 5432986 04 A B Couldnt get his webbing out
Parker, Peter 05 A G Lost his camera
Man, Super 98 A T Kryptonite was under his bed
Clark, Kent 43 F R Lost Keys
Pool, Dead 69 F U Lost voice
Wilson, Wade 69 U F 9:15 - 10:15 Dr's

where.txt
Code:
Rogers, Steve
Parker, Peter
Clark, Kent
Wilson, Wade


Last edited by pludi; 03-13-2012 at 10:51 AM..
# 2  
Old 03-13-2012
use grep -f <file with patterns to find>
Code:
grep -f where.txt heros.txt > newfile.txt

# 3  
Old 03-13-2012
It is just printing the heros.txt file. I even tried (awk '{ print $1, $2 }' heros.txt | grep -f where.txt) but it also just printed heros.txt information.

How do I get it to search the names from lines in wheres.txt file against the hereos.txt file to output the matching names?
# 4  
Old 03-13-2012
Jim's command worked for me to select just the particular lines from heros.txt based on what is in where.txt. It does output the whole line from heros.txt, but only the lines with a matching hero name. Are you wanting it to only output the names? or does grep not work as expected for you?

You also mention "for loop". Are you trying to program this in some language? Which language?
# 5  
Old 03-13-2012
I am making a bash script. I need to scan to see if people are on an absent list and the last part gives a description of why they are not there. I took me a minute to get it from a pdf file to a .txt file to scan. In the program I want to run it ./program todaylistpm.pdf and it will convert and find the user. Maybe have it send it to me as an email down the road..

When I run
Code:
grep -f where.txt heros.txt > newfile.txt

I get :
Code:
America, Captain 54329876 09 A A Sick
Rogers, Steve 542986 09 A A NOT HERE
Man, Spider 5432986 04 A B Couldnt get his webbing out
Parker, Peter 05 A G Lost his camera
Man, Super 98 A T Kryptonite was under his bed
Clark, Kent 43 F R Lost Keys
Pool, Dead 69 F U Lost voice
Wilson, Wade 69 U F 9:15 - 10:15 Dr's

GNU grep 2.5.3: grep version

Last edited by Franklin52; 03-14-2012 at 05:08 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 03-21-2012
Ok, after installing Centos 6.2 I found that grep worked the way it should have. I dont think it like what ever what in my ubuntu box.
# 7  
Old 03-21-2012
Ref post #5. Should be grep -F not grep -f .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question about for loop

Hi, I have code like below disk_list=$(ls /root/file1) for disk in $disk_list do pvcreate $i done I know what pvcreate command does, but I do not understand what this $i do here. can someone please explain. (2 Replies)
Discussion started by: stew
2 Replies

2. UNIX for Dummies Questions & Answers

Loop question

Hi, I would really appreciate some help with manipulating a file. Here is my input file: SNP_1 : 1 SNP_11 : 1 SNP_2 -0.12 1 SNP_12 -0.3472 1 SNP_3 -0.708 1 SNP_13 0.5037 1 SNP_4 1.492 0.5 SNP_14 -0.0685 1 SNP_5 1.27 0.5 SNP_15 0.547 ... (3 Replies)
Discussion started by: zajtat
3 Replies

3. Shell Programming and Scripting

While loop Question

Hi, I have a requirement where I have to check for 10 files in Unix location. If all of the files present in that directory then i have execute another process. can you help me resolving this issue. sample location /home/usr abc.txt cde.txt aaaa.txt lll.txt ooo.txt if all the... (3 Replies)
Discussion started by: manasvi24
3 Replies

4. Shell Programming and Scripting

For loop question

I have two files. In file one, there are many columns, but only two of interest to me. Column 1 contains a list of individuals, defined by an ID number. Column 10 contains the diagnosis that each individual has (I am a physician). All together, there are 3000 lines in this file, one line per... (2 Replies)
Discussion started by: awc228
2 Replies

5. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

6. Shell Programming and Scripting

loop question

hey guys what im trying to do is do a simple script that will ask for a password and on the 5th time it says access denied if the right password is still not entered this is what i have so far can anyone help me im not good with scripting thanks in advance #!/bin/bash secretname=secret... (2 Replies)
Discussion started by: randallrivy11
2 Replies

7. Shell Programming and Scripting

For Loop Question

I'm improving the way an existing script handles arrays, but the results aren't what I had in mind: e="Too many consecutive errors... System is probably unstable!" e="Cancelable Timer Wait Failed!" for errcd in ${e} do echo ${errcd} done The for loop interprets the spaces... (2 Replies)
Discussion started by: ironhalo
2 Replies

8. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

loop question

HI ALL How to read first 10 line from a file using any method (5 Replies)
Discussion started by: aliahsan81
5 Replies

10. Shell Programming and Scripting

Loop question

hi, how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen? thanks eg: #!/bin/sh FILE="somefile.txt" text_line="" what kind of loop would use here? (18 Replies)
Discussion started by: strike
18 Replies
Login or Register to Ask a Question