LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2014, 10:43 AM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Rep: Reputation: Disabled
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.
 
Old 11-18-2014, 12:05 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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
 
Old 11-18-2014, 12:11 PM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-18-2014, 12:29 PM   #4
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
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.
 
Old 11-18-2014, 12:38 PM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
This actually was working. Just an oversight concerning the wrong IP address.
 
  


Reply



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
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 5 06-14-2019 08:18 AM
How does the sed buffer space work? ShanxT Programming 5 10-11-2011 10:53 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
how do other commands work with 'sed' sumitdevbharadwaj Linux - General 2 07-29-2009 09:42 AM
can't get non-greedy matching to work in sed lumix Linux - Newbie 3 05-05-2008 07:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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