LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 01: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, 01:27 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
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, 02:00 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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, 02:09 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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, 04: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 04: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 02:25 PM
shell (bash) scripting - strings vs. integers bullfrog1870 Linux - Newbie 3 11-02-2006 08:51 PM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 10:26 AM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM
adding strings in bash FireAge Linux - General 4 03-11-2003 10:57 AM


All times are GMT -5. The time now is 12:37 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration