LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-28-2010, 10:30 AM   #1
soplin
Member
 
Registered: Apr 2007
Distribution: Mostly Debian
Posts: 114

Rep: Reputation: 19
Sed


How could I replace text in one file with text from another file using sed?

The text in each of the files would be surrounded by a starting and ending delimiter or a starting tag, say like
/* */ so to easily find them.

thank you

Last edited by soplin; 04-28-2010 at 10:31 AM.
 
Old 04-28-2010, 12:08 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Could you be a bit more specific please? How about posting some actual examples of the kind of text that you would be handling, along with the format of the desired output?

In any case, I'd guess that the most likely procedure would be to use a tool like grep, sed, or awk to extract the text you want from one file, store it in a variable, then use sed or awk again to insert the contents into the second one. But the devil's in the details, so to speak.

Last edited by David the H.; 04-28-2010 at 12:14 PM. Reason: Added follow-up paragraph.
 
Old 04-28-2010, 02:26 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by soplin View Post
How could I replace text in one file with text from another file using sed?
thank you
  1. read the text from the second file
  2. process as required and put into one or more variables
  3. run SED on the first file and use the content of the variables to make the required substitutions.

But, perhaps you meant replace all of the contents--using all of the contents of the other file. (In that case, don't use SED)
 
1 members found this post helpful.
Old 04-28-2010, 03:00 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I'm not sure if this is along the lines of what you are asking about; but I once created a SED script to take the output of the "file" command and produce a script to rename files, adding the extensions, based on the mime database. The SED program was over 800 lines long. I massaged the mime database to a convenient form, using SED and vim, and then used a SED command to create the final SED program. Running the final SED program would result in a bash script that I would run to rename the files (after inspection). This was for a person who recovered files from a corrupt filesystem and was left with thousands of file names without extensions.

Whether you use sed, awk or perl, regular expressions need to be precisely described to handle your exact situation and to avoid missed patterns or false positives. As a result, you need to be more exact in defining your question, and include actual lines of input, and the output you want to produce.
 
1 members found this post helpful.
Old 04-28-2010, 07:46 PM   #5
soplin
Member
 
Registered: Apr 2007
Distribution: Mostly Debian
Posts: 114

Original Poster
Rep: Reputation: 19
operation.txt >

The Operation Story

Chapter1
Performing a kidney removal on myself without anesthetics.

Chapter1


storyChanger.txt >

How to Eat Yourself or Someone Else

Chapter1
If you are eating yourself, start by cutting off your foot.

Chapter1


****

What's in between chapter1 of storyChanger.txt should replace what's in between chapter1 of operation.txt .

This is just for my personal practice. I keep coming back to GUI even though I want to go as long as possible using CUI except for when I have to use GUI like watch a movie. It is some of these commands or tasks that get in my way.

Last edited by soplin; 04-28-2010 at 08:11 PM.
 
Old 04-29-2010, 12:29 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Look at selecting line ranges in sed. e.g. /Chapter1/,/Chapter1/

also look at the read command. Rather than using a single sed command, perhaps use sed to extract the replacement test, and extract it to a temporary file (or a pipe) and a second sed command to locate the range of lines in the original text, and read the lines from the temporary file.

If you do do something like this, perhaps using xml markup for both files, would be a better plan.
 
1 members found this post helpful.
Old 04-29-2010, 08:04 AM   #7
soplin
Member
 
Registered: Apr 2007
Distribution: Mostly Debian
Posts: 114

Original Poster
Rep: Reputation: 19
Alright, thanks. I'll be looking through more commands and combine.
 
Old 04-29-2010, 09:58 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd start by figuring out how to match the text you want to change in both files. Check out the sed faq for some useful tips on handling common situations.

When you've made some progress there we can see about building a script around them to copy the text between them.

BTW, please use [code][/code] tags when posting any blocks of text where the formatting is important (script code and file contents). It helps keep things readable. Thanks.
 
1 members found this post helpful.
Old 05-01-2010, 02:40 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
For real documents on a large project, you would probably have a text file for each chapter and use a text editor to make your changes. Vim's ed commands are nearly identical to using sed.
 
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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
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 - General

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