Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH FOR loop using grep and awk

I'm trying to get this script to cat a file and grep each line for 877 and for each line found, print the first column which is an IP and store it in hosts. It get stuck on awk every time. I run sh -x some.sh to see where it is hung up. Should I print to a file instead of a list? Why does it get stuck on awk?

hosts=()

FILENAME=/home/somethin/.hosts.conf

ips=`cat $FILENAME | grep -v '877'`

for line in $ips; do
     hosts=$(`awk '{print $1}'`)
done

echo $hosts
like image 658
cvjones360 Avatar asked Dec 28 '25 03:12

cvjones360


1 Answers

It can all be done using awk:

hosts=( $(awk '/877/{print $1}' $FILENAME) )

echo "${hosts[@]}"
like image 178
anubhava Avatar answered Dec 30 '25 21:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!