Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-19-2012, 03:29 PM   #1
LQ Newbie
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
Bash Populate array and ignore lines with certain characters

I'm trying to read a DNS file with a list of hosts into an array. I think I have the array populated, but I would like to ignore lines with certain chars, such as semicolon, at (@) sign, etc. I'm not sure how to do so. Can you point me in the right direction?

Right now, the below populates everything into an array. Thanks!

array=( $(ssh $USER@$DNS_SERVER "cat /var/cache/bind/dns_hosts | awk '{print \$1}'") )

echo ${array[@]}
Old 11-19-2012, 03:59 PM   #2
LQ Newbie
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I think I figured it out, but just in case anyone has better suggestions, I'm all ears:

array=( $(ssh $USER@$DNS_SERVER "cat /var/cache/bind/dns_hosts | grep -v ';' | grep -v '@' | awk '{print \$1}'") )

Old 11-19-2012, 08:14 PM   #3
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,091

Rep: Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273
awk generally subsumes grep's functionality:
grep -v ';' | grep -v '@' | awk '{print $1}'
## can be written as
awk '!/;|@/{print $1}'
## or
awk '!/[;@]/{print $1}'
Also, you have a Useless Use of Cat:
cat file | command
## can be written as
< file command
## when command is awk, you can also use
awk '{awk code}' file
array=( $(ssh $USER@$DNS_SERVER "awk '!/[@;]/{print \$1}' /var/cache/bind/dns_hosts") )


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
sed match and ignore new lines ted_chou12 Programming 5 02-26-2012 11:33 AM
[SOLVED] Searching for files and populate in array Jakkie Linux - Newbie 14 01-26-2012 08:38 AM
[SOLVED] How can I ignore lines with partial similarity and retain only one. btacuso Linux - Newbie 3 03-13-2010 02:34 PM
splitting numbers and characters from an array of characters. trscookie Programming 6 11-14-2008 09:34 AM
Another Bash iterate lines to array... jchambers Programming 9 08-22-2007 02:06 PM

All times are GMT -5. The time now is 04:15 AM.

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