LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-17-2008, 08:23 PM   #1
stlouis
Member
 
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63

Rep: Reputation: 16
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
 
Old 01-17-2008, 09:27 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,336

Rep: Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176
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.
 
Old 01-17-2008, 09:31 PM   #3
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
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.
 
Old 01-17-2008, 09:50 PM   #4
stlouis
Member
 
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63

Original Poster
Rep: Reputation: 16
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 in BASH Script - Syntax Problem stlouis Programming 4 01-13-2008 11:38 AM
question about C++ syntax bx.s Programming 3 07-27-2006 08:50 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
Syntax question satimis Linux - Newbie 9 09-23-2004 08:47 AM
Syntax question satimis Linux - Newbie 4 10-08-2003 10:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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