Search for names in one list in another and print results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for names in one list in another and print results
# 1  
Old 07-23-2018
Search for names in one list in another and print results

Hi All ,



New to the Bash / Shell programming world and looking for some help

I have two files



1: Contains a list of names :

eg

Code:
STEVE
BOB
CRAIG

2: Contains information with those included names but also others that are not in the list

Code:
STEVE     USER1    0987
ALUN       USER2   0762
BOB        USER3    9327
CRAIG     USER4     3427


I am looking to Cat the first file then grep the names in that list and output the results from file 2

Code:
for i in `cat list1 ` do egrep -i $i list2 ; done

The above just seems to loop through the second list numerous times until it has reached the end of file 1 without giving me the information

Cheers for the anticipated help




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-23-2018 at 08:25 AM.. Reason: Added CODE tags.
# 2  
Old 07-23-2018
Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

What exactly is going wrong with your code snippet - after having added a semicolon in front of the do? And, except this being incredibly inefficient. When I run it, it gives me the file2 lines for the names in file1.

Did you consider sth. like
Code:
grep -ffile1 file2
STEVE     USER1    0987
BOB        USER3    9327
CRAIG     USER4     3427

?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. What is on Your Mind?

Search Results (Search, New, and Today's Topics) Animation Switch

Hey, I added an animation switch on the search results page; so by default the thread previews are off, but if you want to look at them, just click on the green button and the thread previews will turn on (and back off). See image and attached animation: ... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Finding a string in a list of files, print file names

I'm interested in writing a report script using BASH that searches all of the files in a particular directory for a keyword and printing a list of files containing this string... In fact this reporting script would have searches for multiple keywords, so I'm interested in making multiple... (2 Replies)
Discussion started by: chemscripter904
2 Replies

3. Shell Programming and Scripting

how to print off results

print from an ip_list file containing 300 ip's the directory of the results is /var/tmp/1.1.1.1 the 1.1.1.1 will change according to the /tmp/ip_list file i.e 1.1.1.1 2.2.2.2 3.3.3.3 I need the results from /var/tmp/1.1.1.1 once done the script goes to the next ip address in... (11 Replies)
Discussion started by: slashbash
11 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. Shell Programming and Scripting

How to print sed results

Dear all, How can I print results (and of course, send this result to the text file) of sed command. I mean, I want to know which lines of which files sed command has found. For e.g, the result text file should contains: file1.c:line 12 file2.h:line 14 file2.h:line 37 Please help me (10 Replies)
Discussion started by: Hannibal2010
10 Replies

6. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

7. Shell Programming and Scripting

Print just 1 results from the whole line

Hi All, need some help with writing a shell script. I tried to search this forum but couldn't find exactly what i want. If you all know any reference link i can read and refer to solve my issue, let me know. I got 1 file i.e: example.txt. It is content list of data, as below example. What... (2 Replies)
Discussion started by: anakiar
2 Replies

8. Shell Programming and Scripting

search file and print results with shell script

input file 1.<CRMSUB:MSIN=0100004735,BSNBC=TELEPHON-9814060328-TS11&TS21&TS22,NDC=9814,MSCAT=ORDINSUB,SUBRES=ALLPLMN-SPICE,BAOC=OIC,BAPRC=INFO,ACCSUB=BSS,NUMTYP=MULTI;... (3 Replies)
Discussion started by: dodasajan
3 Replies

9. Shell Programming and Scripting

AWK - no search results

Hi all, I'm new to awk and I'm experiencing syntax error that I don't know how to resolve. Hopefully some experts in this forum can help me out. I created an awk file that look like this: $ cat myawk.awk BEGIN { VAR1=PATTERN1 VAR2=PATTERN2 } /VAR1/ { flag=1 } /VAR2/ { flag=0 } {... (7 Replies)
Discussion started by: hk18
7 Replies
Login or Register to Ask a Question