LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-03-2011, 01:18 AM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
writing to a file using sed


Hi all,

i am having a the following line in a file

<property name="connection.password"></property>

i have to give password in this line, like this

<property name="connection.password">XXXXXX</property>


Is this possible to acheive this using sed command

Beause i have to do this for n number files. so if i have option in sed for this the i will be doing this using script.


Your suggestions please.

Thanks&regards,
Dinesh.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-03-2011, 01:29 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

If that's the only instance in a file then you can use sed like this:
Code:
sed -i 's/connection.password">/connection.password">YOURPASSWORD/g' filename
which will add your password in plain text in the file 'filename'. A great introduction and tutorial for Sed can be found here.

Kind regards,

Eric
 
2 members found this post helpful.
Old 03-03-2011, 01:37 AM   #3
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi,

Thanks that works.
 
Old 03-03-2011, 01:52 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

You're welcome. If you consider your problem/question solved then please mark it as such using the Thread Tools.

Kind regards,

Eric
 
Old 03-03-2011, 02:49 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Small tweak you can use also:
Code:
sed -i 's/connection.password">/&YOURPASSWORD/g' filename
 
1 members found this post helpful.
Old 03-03-2011, 03:36 AM   #6
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi,

Sorry i have one more question. I am having the line as

<property name="connection.password">root</property>


in the file. I have to change the root to some other password. Can u please explain how can we do that using sed.

I know we can do that by replacing the string root from that line with another string using sed.

But i am having more string with the name root, but i need to change only this particular string.

Can u please post your suggestion.
 
Old 03-03-2011, 05:32 AM   #7
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi all,

Thanks Finally i got it worked by.

sed -i 's/connection.password">root/connection.password">MYNEWPASSWORD/g' filename
 
Old 03-03-2011, 07:04 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Glad you got it You can also use back referencing:
Code:
sed -r -i 's/(connection.password">)root/\1MYNEWPASSWORD/g' filename
 
1 members found this post helpful.
  


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
[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
[SOLVED] Sed – how do I save output to file with filename from content of another file? misarab Programming 4 02-28-2011 02:12 PM
sed delete lines from file one if regexp are listed in file two fucinheira Programming 6 09-17-2009 08:28 AM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
Writing shell script with sed,sql, and epoch milliseconds Eamo Programming 7 07-16-2007 03:40 PM


All times are GMT -5. The time now is 06:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration