LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-10-2006, 01:03 PM   #1
GUIPenguin
Member
 
Registered: Aug 2004
Location: Maine
Distribution: Gentoo Linux
Posts: 239

Rep: Reputation: 30
shell script help, remove periods from 123.456.789


What I have now is a simple line to grep, awk and cut an IP to display to the console

/sbin/ifconfig eth1 | grep addr | awk '{print $2}] | cut -c5 | cut -c2-12


this will give me 9 digets of my ip, such as 169.244.255 because that is simuler to our school subnet and is how I need it. I know there is prob an easer way.. but I just whiped that together and am not looking for a different way.

now how can I remove those dots so I just get 169244255 ? Much Thanks.



EDIT (Solved) I used sed (although it confuses the heck out of me lol)
| sed -e 's/\.//g' will give me what I was looking for.

Last edited by GUIPenguin; 04-10-2006 at 01:40 PM.
 
Old 04-10-2006, 01:59 PM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Code:
... | sed -e 's/\.//g'
 
Old 04-15-2006, 09:42 PM   #3
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
You could have also used gawk (or awk) like this:

gawk -F. '{print $1$2$3$4}'

The -F. sets the field separator to a period. When you list the output fields one after another they are concatenated. Many, many ways to skin the cat in Linux.

Another way to do what you want is to use extended regular expressions (regexp) with grep as follows:

/sbin/ifconfig eth1 | grep -o -E [[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{3}

This will effectively strip out all xxx.xxx.xxx.xxx number sequences from your output. The [:digit:] regexp is any digit (go figure). The bracketed number, {3}, following the [:digit:] regular expressions says to find exactly 3 occurrences of this regexp. The \. is an escaped period, since a period has a special meaning. Therefore, you need to escape it to tell grep I really want a period, not it's special meaning. And of course, this is repeated 3 more times to form a 4 number sequence of 3 digits separated by periods, i.e. xxx.xxx.xxx.xxx. IP's may only have one digit for one of the numbers, e.g. 192.168.1.101. To filter out this IP, change the {3} to {1} for the 3rd regexp:

/sbin/ifconfig eth1 | grep -o -E [[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{1}\.[[:digit:]]{3}

To mix things up a little more, you can remove all the {3} and replace it with a +. The + means find atleast one occurence or more of the regexp preceeding it. So,

/sbin/ifconfig eth1 | grep -o -E [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+

would find things like xx.xx.xxx.x or xxx.xxx.x.x and so on. You should get the point by now. Regexp are pretty powerful. Hope this helps someone who may want to do similar things. All the best.

Last edited by card-suse; 04-16-2006 at 12:06 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 03:47 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Removing 4k stack size limit on 2.6.7-1.456 Corallis Fedora 2 07-23-2004 09:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration