LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with bash and strings (https://www.linuxquestions.org/questions/programming-9/need-help-with-bash-and-strings-564891/)

Histamine 06-27-2007 01:12 PM

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

unSpawn 06-27-2007 01:27 PM

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 ?

pixellany 06-27-2007 02:00 PM

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)

pixellany 06-27-2007 02:09 PM

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

Histamine 06-27-2007 04:18 PM

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!


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