The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Linux sed command [SOLVED]

AthlantarAthlantar Registered User regular
edited December 2008 in Help / Advice Forum
Hi all,

I suppose a linux forum would be abetter place to post this, but I love you all so much here.
I'm trying to write a bash script to take an ip address, and give me each component. This isnt for an assignment or anything, just for a personal project.

I've been messing with that damn sed command for hours trying to split the text up properly.
For instance, if I had the ip address 123.58.93.923 I would like one line of code to give me
123
another line to give me
58
another line to give me
93
and another to give me
923

I cant seem to write a piece of code that will isolate each component, and be able to handle if correctly if theres a different number of characters in each segmet. So if i refreshed my dynamic IP and it became 11.539.2.99, i woudl like the code to still break it up.

Perhaps this task is more difficult than its worth, but if anyone has a quick and easy solution, I'd be very appreciateive. the base code I've been working with is
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Thanks amillion if anyone has any suggestions!

10088847214381951643320470475858305731166183151407164751271470824235003318621252307969752086088076499395823874814123350292603347408732347765156628342107995
Athlantar on

Posts

  • BarrakkethBarrakketh Registered User regular
    edited December 2008
    Why restrict yourself to sed? I changed the sed command to egrep, but you can still substitute any command that will return an IP address.
    wget -q -O - checkip.dyndns.org | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk -F '.' '{print $1"\n"$2"\n"$3"\n"$4}'
    

    I typically use awk to split along whitespace (the default), but you can change the field separator with "-F".

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • ecco the dolphinecco the dolphin Registered User regular
    edited December 2008
    If you really want to use sed, you can go:
    sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1/g'
    sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\2/g'
    sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\3/g'
    sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\4/g'
    

    I'd recommend using "cut", actually. Slightly shorter than the sed and awk.
    cut -d . -f 1
    cut -d . -f 2
    cut -d . -f 3
    cut -d . -f 4
    

    e.g. echo 213.4.55.11 | cut -d . -f 1
    Athlantar wrote: »
    I've been messing with that damn sed command for hours trying to split the text up properly.
    For instance, if I had the ip address 123.58.93.923 I would like one line of code to give me
    123
    another line to give me
    58
    another line to give me
    93
    and another to give me
    923

    (bolded my emphasis) If you ever get anything like that, then the Internet has broken. :P

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • AthlantarAthlantar Registered User regular
    edited December 2008
    Thanks a ton you guys. Those work exactly like I wanted them to and provide me some very useful insight into the syntax of those seemingly simple but incredibly complex commands.

    Thanks again! its great! :D

    Athlantar on
    10088847214381951643320470475858305731166183151407164751271470824235003318621252307969752086088076499395823874814123350292603347408732347765156628342107995
This discussion has been closed.