trim last octate of ip address using bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trim last octate of ip address using bash script
# 1  
Old 06-23-2009
trim last octate of ip address using bash script

Hi,
i need to replace the last octate in ipaddress with 0 using bash shell for one of my applicatiom.
googling i found the below link where they do the same thing but use long2 ip which i dont see in linux.

trim ip address octet - Stack Overflow

Plz can soemone guide how do i do this using bash shell script?

Thanks in advance..
# 2  
Old 06-23-2009
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future
All firsts posts from new members are moderated (especially if a link is included!) so please be patient, a moderator has to approve your post before it appears in the forum...
Thank You.

The UNIX and Linux Forums.
# 3  
Old 06-23-2009
here is a simple shell test.sh to do what you want
Code:
cat test.sh
ip=$1
baseip=`echo $ip | cut -d"." -f1-3`
echo $baseip".0"
./test.sh 192.168.133.14
192.168.133.0

# 4  
Old 06-25-2009
wow!! thanks a lot Smilie
# 5  
Old 06-25-2009
Or ...

Code:
echo 192.168.133.14 | sed 's/\.[0-9]*$/.0/'

# 6  
Old 06-25-2009
ok. thanks a lot.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. Shell Programming and Scripting

Bash to trim folder and files within a path that share a common file extension

The bash will trim the folder to trim folder. Within each of the folders (there may be more than 1) and the format is always the same, are several .bam and matching .bam.bai files (file structure) and the bashunder that executes and trims the .bam as expected but repeats the.bam.bai extentions... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

Bash script who maps IP with MAC address

Hy every body, Unfortunately and without success, i want to write a bash script who maps a known IP addess to a known MAC address using iptables and for the FORWARD chain. Within the DHCP server, i have assigned a fixed IP address to all clients based on their MAC addresses of their... (11 Replies)
Discussion started by: hermouche
11 Replies

4. Shell Programming and Scripting

IP Address LookUp Bash Script

I am new to bash scripting. I want write a script that reads from the first argument file and run nslookup, then prints out each nslookup. Something like below: File name = ip 8.8.8.8 8.8.4.4 Bash shell script: nslookup.sh #!/bin/bash for i in $1 do nslookup $i done I... (7 Replies)
Discussion started by: boldnbeautiful
7 Replies

5. Shell Programming and Scripting

TRIM a string in shell script

HI, I have a string "NZ-deploy-mode-1.15-Linux.x86_64.rpm" I want to get the string before -1 ie "NZ-deploy-mode" Input is "NZ-deploy-mode-1.15-Linux.x86_64.rpm" expected output is "NZ-deploy-mode" How can I do that in shell script? Thanks in advance. (6 Replies)
Discussion started by: Ananthdoss
6 Replies

6. Shell Programming and Scripting

Can i use trim in shell script ?

Hi, I am stuck at comparing a value from database with a space involved. Condition if (cust_code != "RAMK" && cust_code != "LML") { xxxxxxx xxxxxxx xxxxxxx } the cust_code is from a table and generally has 4 chars...so in the case of LML it comes with default space at the end. ... (1 Reply)
Discussion started by: ramkiran77
1 Replies

7. Shell Programming and Scripting

How to trim a string in unix shell script

I am reading a string like $/folder1/folder2/filename from a file And I am storing it in a variable say $path. I want to store the path within single quotes in order to use the path within a command. if i set like path="'"$path"'" echo $path It is printing like ' $/folder1/folder2/filename'... (6 Replies)
Discussion started by: Shri123
6 Replies

8. Shell Programming and Scripting

Need help in shell script to trim numeric values

Hello I am currently working on a shell script which actually needs to pull some file from a server , The filenames will have the extension with date/time stamp , i need to trim that and name it to proper format Example : xyz_20091108_22142365.gzip i need to remove the... (5 Replies)
Discussion started by: ranga27
5 Replies

9. Shell Programming and Scripting

check if argument is an ip address in bash/sh

Hi all, Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh? Thanks, Marc (3 Replies)
Discussion started by: marcpascual
3 Replies

10. Shell Programming and Scripting

Reversing and trim a String through SHELL script

All I want to reverse and trim a string using UNIX shell scripts.Iam using Bourne shells. Can you help me? Thanx in advance Regards Deepak (5 Replies)
Discussion started by: DeepakXavier
5 Replies
Login or Register to Ask a Question