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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-17-2008, 08:23 PM
|
#1
|
Member
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63
Rep:
|
Another SED Syntax Question...
Hi everyone,
I have another sed question for everyone... I'm having a bit of troubles with something, and it's really becoming frustrating...
I'm trying to do the following:
I have a plain text table that has entries in the following format:
26:<IP_ADDRESS>
27:<IP_ADDRESS>
and so on....
The 26 and 27, etc... at the beginning are just User Numbers
Since users ip's are dynamic, they are always changing, so I needed a way to keep this table updated when the user log's on. So I'm writing a script that will be run from the user's .bash_profile at login, which will update this table.
There is a constant variable, lets say $myuser which returns the appropriate usernumber, eg. 26, 27, 28, etc....
I defined a variable called $myip which obtains the user's IP Address in the following way:
myip=`echo $SSH_CLIENT | awk '{print $1}'`
Now I was going to use sed to update the table, by searching for the constant variable, which is the usernumber, the replace the existing IP Address with the new one, if it is different. Even if it just updates the substitutes the IP, regardless if it changed or not, avoiding the additional check would be OK, as the system has a handful of users.
The problem is, I can't seem to figure out the proper syntax for doing this with sed.
eg.
26:192.168.2.100 updated to 26:192.168.2.101
With the two variables I'm calling $myip and $myuser and the : , I can't seem to figure out how to do this right...
Sorry if I put more detail than needed, I'm still in the process of learning SED, and didn't want to leave nothing out...
Jeff
|
|
|
01-17-2008, 09:27 PM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,336
|
Something likethis might work
Code:
sed -i -e "/$myuser/s/:.* /:$myip /" infile
uses $myuser to address the line you want. Presumes well-formed data - you could easily make the test more robust.
|
|
|
01-17-2008, 09:31 PM
|
#3
|
Member
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Rep:
|
sed:
Code:
myip=192.168.0.69
myuser=9
sed -i "s/^\($myuser:\).*/\1$myip/" tablefile
remove the -i to test it.
Edit: doh, too late.
Last edited by angrybanana; 01-17-2008 at 09:32 PM.
|
|
|
01-17-2008, 09:50 PM
|
#4
|
Member
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63
Original Poster
Rep:
|
Quote:
sed:
Code:
myip=192.168.0.69
myuser=9
sed -i "s/^\($myuser:\).*/\1$myip/" tablefileremove the -i to test it.
Edit: doh, too late.
|
Worked like a charm... I had a few examples similar to that, but wasn't using the brackets. Definitely didn't use the -i for testing... I only posted that way for purpose of completeness...
|
|
|
All times are GMT -5. The time now is 07:47 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|