Awk: Help with how to remove 4rth octet :


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Help with how to remove 4rth octet :
# 1  
Old 02-20-2013
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:
Code:
# awk '{ sub(/\.[0-9]+[ ]/,x,$3); print $2,$3}' 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-20-2013
Using sed:

Code:
$ sed 's/^[^=]*= \([^ ]*\)[ ]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\..*$/\1 \2/' foo.txt 
cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1

Cheers,
ZB
This User Gave Thanks to zazzybob For This Post:
# 3  
Old 02-20-2013
Your awk program were working fine if you searched for the end of $3, represented by "$", instead of looking for whitespace which, being the field separator, was stripped off:
Code:
$ awk '{ sub(/\.[0-9]+$/,x,$3); print $2,$3}' file
cmiHOST06 10.26.107
cmiHOST05 10.26.12
cmiHOST05 10.26.1

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-20-2013
zazzybob,

- Thanks..

Code:
sed 's/^[^=]*= \([^ ]*\)[ ]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\..*$/\1 \2/' foo.txt

The code Looks very complicated, But it worked perfectly , can you please explain a bit.

---------- Post updated at 05:40 PM ---------- Previous update was at 05:30 PM ----------

RudiC,
Thanks it worked very well, thanks for the $ at the end.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash subtract fourth octet of an IP by 1

Hello, Im looking to help out my team by automating a simple search list. The user will look for a peering ip /30. For example 192.168.1.2/30 and gets the result. Im trying to get the entered /30 and subtract the last octet by one. echo -n "Enter peering ip : "; read peeringip cat... (3 Replies)
Discussion started by: D'go
3 Replies

2. Shell Programming and Scripting

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 # awk '{print $2, substr($4,1,9)}' file . but not correct File: HOST= cmiHOST06 :: 10.26.107.73:/data120 /nbu/cmiHOST06/athpx07/aa1 HOST= cmiHOST05 :: 10.26.12.76:/data120... (5 Replies)
Discussion started by: rveri
5 Replies

3. UNIX for Dummies Questions & Answers

What is an (application/octet-stream) file?

I'm trying to learn as much about GRUB as I can and it's stages are stored in these types of files. Any info or search terms is appreciated!:wall: (5 Replies)
Discussion started by: theKbStockpiler
5 Replies

4. 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

5. Programming

ip address octet increments

Hi all, Situation is as below. I would get an IP address and port from eithe r a file or command line. It probably would be as char * or string. So was wondering how I could accept this and increment the last octets? Incrementing the port is fine. I could get that into an integer by atoi()... (8 Replies)
Discussion started by: Naanu
8 Replies
Login or Register to Ask a Question