bash scripting and awk help

Hey guys, i am fairly new to scripting and I am trying to write a script that takes a comma delimited file as input. I am trying to figure out a way to determine if $1 and $3 exist on a line (basically a hostname and ip address) and if true do the following,
resolve the hostname to ip.

sample file looks like this:

hostname.blah.blah.com,blah,10.x.x.x

I am able to awk the file and separate the hostnames and ip's using the following

cat $filename | awk -F "," '{print $1}'
cat $filename | awk -F "," '{print $3}' | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[\
0-9]*$')

But I am trying to to do this in 1 line, and I'm not sure of the proper syntax and determine whether they both exist on the same line, and if true do the next step.

Thanks in advance.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 05:34 PM ---------- Previous update was at 05:31 PM ----------

cannot you simply do this:

awk -F, '{print $1, $3}' "${filename}"

I'm not quite sure what you're trying to filter out with the trailing grep......

Hi sorry for being such a n00b. I am trying to grep for a valid ip address in the trailing grep, because the $3 field in the input file has other values other than ip addresses and I am trying to filter for valid ip's. Also I haven't got a valid hostname regex working yet for the $1 field.

Which brings me back to:

awk -F, '{print $1, $3}' "${filename}"

I'm mainly stuck on finding if they both exist on the same line and if true resolve the hostname to ip.

validating IP addresses is the 'science' on its own as there're so many conditions to consider.
With your simplistic 'grep', you can do something like this:

awk -F, '$3 ~ "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$" {print $1, $3}' "${filename}"

Is there a way to add an && operator to the above to check for regex on $1? I tried and it doesn't seem to be working correctly.

awk -F, '$1 ~ /abc/ && $3 ~ "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$" {print $1, $3}' "${filename}"

Works fine with Solaris' "nawk"...
Could you elaborate on the 'not working correctly' part, pls?

I got it to work now. However, I am trying to figure out how to enclose this logic into an If statement in bash. Basically, my logic is if a valid hostname and ip appear on the same line then check to see if the hostname resolves to that ip that appears on that line.

I am thinking of passing the hostname ($1) to the host or nslookup command and check if $3 matches the ip address of the output of the host or nslookup command.

thanks