How to ignore comments at the end of the each line?

Hi All,

I am reading the host file by ignoring the comments and write it to the other file. I am reading with regular expression for IP address.

 
grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' $inputFile | awk '{for(i=2;i<=NF;i++)print $1,$i}'  > $DR_HOME/OS/temp

After that am reading each host name and pinging it and matching the IP address in the response with the IP address in the file.

 
while read ip host
do
ip1=$(ping -q -c 1 -W 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')

Now my host file contains the comment at the end of the line also For E.G.

 
10.3.200.51    SQL1 SQL1.com Delta Delta.com ori ori.com                      #TIB Prod

How to ignore the comments in the end while reading the line

 awk -F"#" '{print $1}' filename 

Or you can modify your awk statement to as below

awk '{for(i=2;i<=NF;i++) {if (substr($i,1,1)=="#") next; else print $1,$i}}'
1 Like

Thanks Kris, Its working fine for me.

Or remove the comment upfront so you don't need to check for it in every loop:

awk '{sub (/#.*$/,""); for(i=2;i<=NF;i++) print $1,$i}'