LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 11-19-2012, 03:29 PM   #1
jtn703
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
jtn703
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}'") )



Thanks!
 
Old 11-19-2012, 08:14 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,494

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
awk generally subsumes grep's functionality:
Code:
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:
Code:
cat file | command
## can be written as
< file command
## when command is awk, you can also use
awk '{awk code}' file
So:
Code:
array=( $(ssh $USER@$DNS_SERVER "awk '!/[@;]/{print \$1}' /var/cache/bind/dns_hosts") )
 
  


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
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 11:03 AM.

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