LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can't get sed to work (https://www.linuxquestions.org/questions/linux-newbie-8/cant-get-sed-to-work-4175525775/)

battles 11-18-2014 10:43 AM

Can't get sed to work
 
I am getting my current IP address like this:

myIP=$(ip addr show dev eth0 | grep "inet " | cut -d" " -f6 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')

(myIP then = 12.34.567.890)


When I try this, it can't find it to delete it out of the file:
(line in file = '1 12.34.567.890')

sed -i "/$myIP/d" temp.txt # remove my IP for safety


When I do this, it finds it and deletes it:

myIP="12.34.567.890"
sed -i "/$myIP/d" temp.txt # remove my IP for safety


Any suggestions? Thanks.

smallpond 11-18-2014 12:05 PM

I would read the IP using system functions instead of parsing command output:

Code:

myIP=$(perl -MSocket -MSys::Hostname -e '@f=gethostbyname hostname(); print inet_ntoa($f[4])')
echo $myIP
192.168.77.232


battles 11-18-2014 12:11 PM

Nice example, I didn't even know that perl could be executed that way. Unfortunately, the sed still didn't delete the ip address in temp.txt.

battles 11-18-2014 12:29 PM

Quote:

Originally Posted by smallpond (Post 5271468)
I would read the IP using system functions instead of parsing command output:

Code:

myIP=$(perl -MSocket -MSys::Hostname -e '@f=gethostbyname hostname(); print inet_ntoa($f[4])')
echo $myIP
192.168.77.232


Incidentally, this gives my address as: 127.0.0.1. Mine gives the correct IP.

battles 11-18-2014 12:38 PM

This actually was working. Just an oversight concerning the wrong IP address.


All times are GMT -5. The time now is 11:17 PM.