LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-25-2015, 04:06 PM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
using sed with variables


I am running these commands to modify a few files

Code:
/bin/sed -i -e "s/ClientX/$client/g" /etc/hosts
/bin/sed -i -e "s/ClientX/$client/g" /etc/hostname
/bin/sed -i -e "s/5040/$port/g" /etc/beatbox/start
/bin/sed -i -e "s/generic/$group/g" /etc/beatbox/start
/bin/sed -i -e "s/ClientX Not Defined/$client/g" /etc/passwd
The files are modified, but where the output of the variables should be, there's only empty space. For example/etc/hostname is empty. It should have the output of $client.

I know my variables are ok because I get the output below.

Code:
if [ -z "$port" ]; then echo "var is unset"; else echo "var is set to '$port'"; fi
var is set to '5074'
root@ClientX:~# if [ -z "$group" ]; then echo "var is unset"; else echo "var is set to '$group'"; fi
var is set to 'generic'
root@ClientX:~# if [ -z "$client" ]; then echo "var is unset"; else echo "var is set to '$client'"; fi
var is set to 'Client024'
Is my sed syntax wrong?
 
Old 08-25-2015, 04:13 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
what does this yield:
Code:
echo client = $client
/bin/sed -e "s/ClientX/$client/g" /etc/hostname
 
Old 08-25-2015, 04:43 PM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
Code:
echo client = $client
client = Client024
Code:
/bin/sed -e "s/ClientX/$client/g" /etc/hostname
<empty line here>
 
Old 08-25-2015, 04:49 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
this worx for me:
Code:
[schneidz@hyper ~]$ cat aristosv.txt 
hello-world-ClientX-l33t-h4x0rz
[schneidz@hyper ~]$ client=Client024
[schneidz@hyper ~]$ sed -e "s/ClientX/$client/g" aristosv.txt 
hello-world-Client024-l33t-h4x0rz
[schneidz@hyper ~]$ sed --version
sed (GNU sed) 4.2.2

Last edited by schneidz; 08-25-2015 at 04:56 PM.
 
  


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
Using SED with variables ? patrick295767 Linux - General 3 08-24-2011 04:10 PM
SED with variables RocketJumper Programming 15 02-06-2011 10:01 AM
Sed and variables oden Programming 9 07-29-2007 04:36 PM
sed with bash variables MurrayL Linux - Software 2 03-21-2005 08:13 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:09 PM.

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