LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2007, 02:12 PM   #1
Histamine
LQ Newbie
 
Registered: Jun 2007
Posts: 20

Rep: Reputation: 0
Need help with bash and strings


I'm trying to write a little script to weed out ip addresses from plain numbers, I've looked around but I'm kind of running into a brick wall. I was trying to go by the period between the numbers ex:

ip or number form matches

192.168.0.243 *.*.*.* yes
1937374087408 *.*.*.* no
8.3.100.5 *.*.*.* yes

also trying to get a tab character in a string subsitution ie:

echo ${x/]/tab tab tab hello}

"\t" doesn't work, I need 8 tabs, any way I can do this?

TIA
 
Old 06-27-2007, 02:27 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
ip or number form matches
Something like "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" ?


I need 8 tabs, any way I can do this?
Could use literal tabs: ctrl+v ctrl+tab ?
 
Old 06-27-2007, 03:00 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by unSpawn
ip or number form matches
Something like "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" ?
/th\a\t\'s ea/\sy f\\or/\\/{you\} to s\\\{{ay\}.....
I thought C was the champion in terms of writing incomprehensible code.

Note that, if you turn on extended REs, eg with egrep, you can shed some "\"s before the "{}"s.

Also, can you not do something like:
"([0-9]{1,3}.){3}[0-9]{1,3}"?
(I have not tried this, so i am using it only to ask the question)
 
Old 06-27-2007, 03:09 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This works:
Code:
sh-3.1$ ifconfig|egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
It turns out you still have to escape the "." when using egrep.
The -o options causes it to print only what it matches---in my case this:
137.79.94.73
137.79.94.255
255.255.255.0
127.0.0.1
255.0.0.0
 
Old 06-27-2007, 05:18 PM   #5
Histamine
LQ Newbie
 
Registered: Jun 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Woop!

That worked, thanxs unspawn!

I just had to enable -e on echo to get the \t tabs to work..

thanks for your help pixellany!

Last edited by Histamine; 06-27-2007 at 05:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: join strings kpachopoulos Programming 1 03-08-2007 03:25 PM
shell (bash) scripting - strings vs. integers bullfrog1870 Linux - Newbie 3 11-02-2006 09:51 PM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 11:26 AM
bash and strings graziano1968 Linux - Software 2 10-01-2004 07:50 AM
adding strings in bash FireAge Linux - General 4 03-11-2003 11:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 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