LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-28-2015, 02:31 AM   #1
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Rep: Reputation: Disabled
SED read text from a file and replace at line 79?


Hi there I have text in a file:

Code:
cat  /etc/openldap/passwd
password
I want to read the string from the file
/etc/openldap/passwd and paste the string in this file /etc/openldap/changes.ldif on line 79 in this format.

Code:
olcRootPW: password

Any ideas?

Thanks
 
Old 07-28-2015, 03:06 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Several.

Since you mention sed in the subject, it will handle it nicely - it accepts addresses, which can be a line number, and bash variables.
You can probably find most of what you need in the sed one-liners .
You make an effort, we'll help with any problems you find insurmountable.
 
Old 07-28-2015, 05:14 AM   #3
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
SED read text from a file and replace at line 79?

how often do you need to do this. if less than monthly just use a text editor which can copy and paste between files. any automation is probably fool hardy can you guarentee that line 79 *always* needs pasting into the other file at the *same* guarenteed point
 
Old 07-28-2015, 05:53 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Ok, I'll bite.

Given that you have a file with this content:
Code:
cat aline.txt 
line_to_copy
You can get that into a variable like this:
Code:
aline=$(cat aline.txt)
echo $aline 
line_to_copy
Now, say that you want to insert at line 3 in this file (to_insert.txt):
Code:
Line 1
Line 2
Line 3
Line 4
Line 5
You can use this sed:
Code:
sed -i.bak "3c $aline" to_insert.txt
Which gets you the following result:
Code:
Line 1
Line 2
line_to_copy
Line 4
Line 5
You will have to implement that in your own script of course.

Best regards,
HMW
 
2 members found this post helpful.
Old 07-28-2015, 08:30 PM   #5
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Original Poster
Rep: Reputation: Disabled
Thanks I was overthinking this:

password="$(cat /etc/openldap/passwd)"
olcRootPW: "$password"
 
  


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] Bash to read text file and replace a line farenheitcx Programming 8 11-19-2011 01:27 PM
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 08:10 AM
how do you replace text between two words in a whole file not just 1 line w/ sed/awk lityit Programming 5 11-04-2011 12:04 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM

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

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