LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-21-2019, 05:15 AM   #1
praveen1461996
LQ Newbie
 
Registered: Jun 2019
Posts: 6

Rep: Reputation: Disabled
replacing a line with sed command


hello ,


i am trying to replace below line using sed


username="test" password="something"

my command is
sed -i -e 's/username=".*"/username="'$username'"/.*\./password=".*"/password="'$password'"/g' server.xml

it is updating username but removing password session from the line
 
Old 06-21-2019, 05:24 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Regex here is greedy - it will look to match the longest string. So the first ".*" will match from the first double quote until the last double quote at the very end - so will wipe out everything after the first equals sign.
You need to be more specific with the pattern.
 
Old 06-21-2019, 05:31 AM   #3
praveen1461996
LQ Newbie
 
Registered: Jun 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
so , can i expect the correct syntax to solve the issue

username and password are variables here

how can i pass variables here

Last edited by praveen1461996; 06-21-2019 at 05:42 AM.
 
Old 06-21-2019, 06:03 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Get the syntax correct first. Then look at fixing the patterns. See also the -r option in the manual page via "man sed" Then look at adding shell variables.

The correct syntax is s///; for each substitution.
The pattern you probably need would be "[^"]+"
Then for the shell variables, modify the quoting. Quotes must be escaped when included inside quotes of like kind.

Also, it will save trouble to specify a backup file: -i.bak or -i.orig or something along those lines.
 
Old 06-22-2019, 01:16 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
.* is any characters; replace with [^"]* (any characters but quote) to not span over a quote.
Code:
sed -i 's/username="[^"]*"/username="'"$username"'"/; s/password="[^"]*"/password="'"$password"'"/' server.xml
In command arguments have $variables in quotes when the shell should not attempt substitutions on it.
 
1 members found this post helpful.
  


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
[SOLVED] sed command for matching the end character of a line and replacing the whole line. mamunm Linux - Newbie 4 04-12-2014 12:19 AM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 07:49 AM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
[SOLVED] SED and Replacing Specific Line bridrod Linux - Newbie 6 08-24-2009 12:28 PM
sed replacing line confusion freeindy Programming 12 08-07-2008 04:49 AM

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

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