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- |
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:
Quote:
|
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. |
rlhartmann, rajwinder:
Outstanding!!!! So simple and yet so usefull..... I love bash.... is there nothing it can't do? :D 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 - |
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 |
Quote:
Thanks much..... |
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. |
All times are GMT -5. The time now is 08:27 AM. |