LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-14-2008, 04:41 PM   #1
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Rep: Reputation: 15
Grep a file with a variable from an array.....


Hi... thanks for reading my thread!

He is my situation..

I have a file that contains several hundred server names along with there associated info.... one line per server.

I sometimes receive a list of servers that are going to be downed and I need to check these against my list for possible impact.

I want to be able to copy the email list into an array, and grep my server list based on the array content and send it to a file.

I am not very good at this yet but am guessing it would be something line this......

array0=(
server1
server2
server3
server4
server5
server6
server7
server8
server9
server10
server11
)

for (( i = 0 ; i < @{#array0[@] ; i++ ))
cat serverlist | grep ${array0[$i]} >> output file
done


Can anyone help with this and explain how it should work?

Thanks,
-OG-
 
Old 06-14-2008, 06:18 PM   #2
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
Assuming your using a bash shell,
I would recommend a simpler for loop

If you already have the list as a string variable (separated by spaces tabs, or newlines)
(SERVERLIST="server1 server2 server3") just put
Quote:

for SERVER in ${SERVERLIST}
do
cat serverlist | grep "${SERVER}" >> output_file
done
If the list of servers going down comes as a file, use
Quote:
for SERVER in $(cat downed_servers_file)
do
cat serverlist | grep "${SERVER}" >> output_file
done

Last edited by rlhartmann; 06-14-2008 at 06:31 PM.
 
Old 06-14-2008, 06:37 PM   #3
rajwinder
LQ Newbie
 
Registered: Jun 2008
Posts: 21

Rep: Reputation: 15
Here is what i understood :

you have one list of servers which are going for maintenance : tmp_list
you have one list having full details on all the servers : org_list

U want read one server at a time from tmp_list and search in org_list ?
If yes then :

cat tmp_list | while read line;do cat org_list | grep "$line";done

should resolve .. let me know how it goes.
 
Old 06-14-2008, 06:49 PM   #4
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
rlhartmann, rajwinder:

Outstanding!!!!

So simple and yet so usefull..... I love bash.... is there nothing it can't do?

Thank you both very much.

BTW.... do you know of a good site or book that I can use to help me with bash? I search google all the time, but I never find gems like this....

Thanks again to you both.

- OG -
 
Old 06-14-2008, 07:37 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
I'm not sure whether I fully understand the task at hand,
but wouldn't it (if file a held the list of the ones to be
downed and b was the complete list) sufficient to do:

grep -f a b

?



Cheers,
Tink
 
Old 06-14-2008, 09:46 PM   #6
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
I'm not sure whether I fully understand the task at hand,
but wouldn't it (if file a held the list of the ones to be
downed and b was the complete list) sufficient to do:

grep -f a b

?



Cheers,
Tink
Wow.... even slicker!

Thanks much.....
 
Old 06-15-2008, 06:16 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Re Bash online tutorials:

http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://rute.2038bug.com/index.html.gz - Linux tutorial from the cmd line

Enjoy


Re rlhartmann; UUOC (useless use of cat)

cat serverlist | grep "${SERVER}" >> output_file

use

grep "${SERVER}" serverlist >> output_file

better performance.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
About the array variable sean_zhang Linux - Newbie 3 02-27-2008 12:01 AM
Grep result passed to variable cornish Programming 5 11-15-2007 05:05 PM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 04:11 PM
Variable Array in C mojozoox Programming 6 12-16-2003 01:39 AM


All times are GMT -5. The time now is 11:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration