awk Quick Help: printing upto 3rd octet .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Quick Help: printing upto 3rd octet .
# 1  
Old 02-19-2013
awk Quick Help: printing upto 3rd octet .

Hi Experts,
I am trying to print $2 & the IP_address upto 3rd octet only.
But unable to do so, Trying
Code:
# awk '{print $2, substr($4,1,9)}' file

. but not correct

File:

Code:
HOST= cmiHOST06 :: 10.26.107.73:/data120 /nbu/cmiHOST06/athpx07/aa1
HOST= cmiHOST05 :: 10.26.12.76:/data120 /nbu/cmiHOST05/athpx07/cc1
HOST= cmiHOST05 :: 10.26.1.75:/data120 /nbu/cmiHOST05/athpx07/dd1



Output should be look like:

Code:
cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1


Thanks a lot,
# 2  
Old 02-19-2013
Code:
awk '{ sub(/\.[0-9]+:.*/,x,$4); print $2, $4}' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-19-2013
bipinajith,
This worked great,
could you please explain a bit this portion , I am unable to unerstand how you got the 3 octet only outof 4 + ":" , with the
Code:
sub(/\.[0-9]+:.*/,x,$4);

Thanks,

---------- Post updated at 10:59 PM ---------- Previous update was at 10:54 PM ----------

Ok got it , that means the 4rth octet and remaining part of $4 substitured with x , that means with nothing value assigned to x, so $4 keeps upto 3rd octet only. Good one. Thanks.
This User Gave Thanks to rveri For This Post:
# 4  
Old 02-20-2013
sub(/ - begin pattern

\. - starts with period .

[0-9]+ - followed by any number (one or more occurrence)

:.* - followed by colon : and zero or more occurance of any char

/,x,$4); - replace pattern with x which is undefined or null, end.

The above pattern actually matches below highlighted and remove them:
Code:
10.26.107.73:/data120

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-20-2013
This is awesome, able to understand now much better. bipinajith Thank you..

---------- Post updated at 11:09 PM ---------- Previous update was at 11:08 PM ----------

I also found another way with split:

Code:
awk '{split($4,x,"\.");print $2,x[1]"."x[2]"."x[3]}' file

---------- Post updated at 11:32 PM ---------- Previous update was at 11:09 PM ----------

Bipinajith,

How the regular expression can be formed if there is no ":" after the 4rth octet:
Below example:

file:
Code:
HOST= cmiHOST06    10.26.107.73 /data120 /nbu/cmiHOST06/athpx07/aa1
HOST= cmiHOST05    10.26.12.76 /data120 /nbu/cmiHOST05/athpx07/cc1
HOST= cmiHOST05    10.26.1.75 /data120 /nbu/cmiHOST05/athpx07/dd1

# 6  
Old 02-20-2013
awk '{ sub(/\.[0-9]+$/,x,$3); print $2, $3}' file
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick awk question

gawk 'BEGIN{count=0} /^Jan 5 04:33/,0 && /fail/ && /09x83377/ { count++ } END { print count }' /var/log/syslog what is wrong with this code? i want to search the strings "fail" and "09x83377" from all entries. im grabbing all entries in the log starting from Jan 5 04:33 to the end of the... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Awk: Help with how to remove 4rth octet :

Experts, In one example I have seen how to get output upto 3rd octet, when there is a ":" separated with the 4rth octet. However in this example how to remove 4rth octet and to keep upto 3rd octet with regular expressions and awk sub function: I have tried with :but not working: # awk '{... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Quick awk tip :)

how do i "awk" the date after the from only to compare it on a if statement later . filename example: server1-ips-ultranoob-ok_From_2012_21_12-23:40:23_To_2012_21_12-23:49:45.zip what i want o do is compare only the date from the string in "From_2012_21_12" in this case i only want the... (4 Replies)
Discussion started by: drd0spt
4 Replies

4. Shell Programming and Scripting

Quick help on 'awk' needed...!!

bash-2.05$ A=`cat /etc/group |awk -F':' '{if ($1$3$4 ==... (3 Replies)
Discussion started by: ak835
3 Replies

5. UNIX for Dummies Questions & Answers

Quick egrep / awk help, Please

Ok, this may be very simple but I can't find a solution. I have a list of numbered values which I have grepped from a larger life. ex/ 1:7.54 2:4.52 3:3.22 4:2.11 5:3.59 6:4.36 7:6.88 8:12.28 9:13.37 10:15.6 11:17.66 12:14.25 I need a quick way to organize them (using awk?)... (4 Replies)
Discussion started by: jdolny
4 Replies

6. UNIX and Linux Applications

Any idea on 3 Octet IP address ?

Hi All, I found my weblog contain entries like 121.23.3 Instead of four octet. I am quite confused is it possible to have 3 octet ip at all ?? Is it generating by any program and hittng the website ? Is it a subdomain ? Please tell me your understanding on it ? Thanks (4 Replies)
Discussion started by: jambesh
4 Replies

7. Shell Programming and Scripting

Help with AWK -- quick question

Ok. I'm just starting to use AWK and I have a question. Here's what I'm trying to do: uname -n returns the following on my box: ftsdt-svsi20.si.sandbox.com I want to pipe this to an AWK statement and make it only print: svsi20 I tried: uname -n | awk '{ FS = "." ; print $1 }' ... (5 Replies)
Discussion started by: Probos
5 Replies

8. Shell Programming and Scripting

printing 3rd or 4th feild from last in awk.

Whats up fellas... hope someone can help me with the following... I am parsing an file that is space delimited, however, in the middle, there is an ugly "Account Name" feild that in itself has multiple varying spaces, and commas which throws off my script. The 1st 3 feilds I am able to obtain... (8 Replies)
Discussion started by: djsal
8 Replies
Login or Register to Ask a Question