Visit Our UNIX and Linux User Community


bash scripting and awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash scripting and awk help
# 1  
Old 09-10-2009
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:

Code:
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
Code:
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.

Last edited by vgersh99; 09-10-2009 at 06:30 PM.. Reason: code tags, PLEASE!
# 2  
Old 09-10-2009
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 [code] and [/code] 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:
Code:
awk -F, '{print $1, $3}' "${filename}"

I'm not quite sure what you're trying to filter out with the trailing grep......
# 3  
Old 09-10-2009
Quote:
Originally Posted by vgersh99
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 [code] and [/code] 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:
Code:
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:
Code:
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.
# 4  
Old 09-10-2009
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:
Code:
awk -F, '$3 ~ "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$" {print $1, $3}' "${filename}"

# 5  
Old 09-11-2009
Quote:
Originally Posted by vgersh99
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:
Code:
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.

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

# 6  
Old 09-11-2009
Quote:
Originally Posted by streetfighter2
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.

Code:
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?
# 7  
Old 09-11-2009
Quote:
Originally Posted by vgersh99
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #159
Difficulty: Easy
The Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) need two ports per host for full-duplex, bidirectional traffic.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash scripting

same script: 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the following lines in... (0 Replies)
Discussion started by: lamoul
0 Replies

2. Shell Programming and Scripting

bash scripting help

Hi Guys i have a <script?> that spits out the location of each printer using snpget here is the code for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
Discussion started by: ab52
2 Replies

3. Shell Programming and Scripting

Bash scripting

Try to imagine a flag: nnnnx nnnxx nnxxx nxxxx now imagine how it will output: 4 times the "n"and 1 times "x" 3 times "n"and" 2 times" x " .. etc. .. rhombus is the same only instead of "n" is there gap "and " x "is a few times to form the correct shape Can you help... (3 Replies)
Discussion started by: krcek12
3 Replies

4. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

5. Homework & Coursework Questions

bash,scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have to do this but i am confused, Create a file containing the bash functions which perform the... (1 Reply)
Discussion started by: CRAZYLITTLELOU
1 Replies

6. Shell Programming and Scripting

scripting help with bash and awk

I'm trying to reformat some tide information into a useable format and failing. Input file is.... 4452 CHENNAI (MADRAS) 1306'N, 8018'E India East Coast 01 June 2009 UT(GMT) Data Area 3. Indian Ocean (northern part) and Red Sea to Singapore 01/06/2009 00:00 0.7 m 00:20 0.7 m 00:40... (3 Replies)
Discussion started by: garethsays
3 Replies

7. Shell Programming and Scripting

please help with Bash Scripting????

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name) ... (2 Replies)
Discussion started by: eminjan
2 Replies

8. Shell Programming and Scripting

bash scripting help!!

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name)... (3 Replies)
Discussion started by: eminjan
3 Replies

9. Shell Programming and Scripting

bash scripting help

hi all i'm trying to get a script working upon connection with pppd According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,... To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables -... (6 Replies)
Discussion started by: jimjones
6 Replies

10. Shell Programming and Scripting

bash/awk scripting help (creating OLD new users)

I need some help making this script... I guess I'm having trouble even interpretating what to even get started on... I need to create a script that will search a given directory (typically a user's home directory, but not necessarily) as provided on the command line and any sub-directors for... (2 Replies)
Discussion started by: Jukai
2 Replies

Featured Tech Videos