sed command to substitute contents from external file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to substitute contents from external file
# 1  
Old 03-21-2013
sed command to substitute contents from external file

Hi All,

I am trying to substitute a line in my existing file with the set of lines from another file. Regarding the existing file, i am sure that the line i am substituting will occur only at one place.

I have stored the text to be replaced in another file.

I am using below command.

Code:
cat InputFile.txt|sed "s/Parameters \"CParameters\"/`cat TextToReplace.txt`/"

I am using cat command to get the contents from second file.

But i am getting below error

Code:
sed: 0602-404 Function s/Parameters "CParameters"/      DSSUBRECORD cannot be parsed.

Here DSSUBRECORD is the first line from my TextToReplace.txt.

Please guide me on this error. Also please suggest on other ways to achieve this substitution requirement.
# 2  
Old 03-21-2013
Code:
sed -e '/Parameters "CParameters"/r TextToReplace.txt' -e "//d" InputFile.txt

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 03-21-2013
Something like this:

File 1:
Code:
$ cat f1
abc
def
ghi

Replacement content file:
Code:
$ cat f2
rep1
rep2

To replace the pattern 'def' in f1 with the file content of f2:

Code:
$ sed -e '/def/r f2' -e '/def/d' f1
abc
rep1
rep2
ghi

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 03-21-2013
First off sorry to the OP.I do not intend to hijack this thread but I only have a similar problemSo thought of asking it here than opening a new thread.

I have the following in a sh script called var
Code:
#!/bin/bash
var1=0

And on another script called main I use a if construct:
Code:
#!/bin/bash
. var
if [ "$var1" == "0"]
Do this
else
do that
fi

Now in do this part,I have to change the value of var1 from "0" to "1".Any help is much appreciated Smilie.Thanks and once again sorry to OP.
Regards,
vijai
# 5  
Old 03-21-2013
Quote:
Originally Posted by hanson44
Code:
sed -e '/Parameters "CParameters"/r TextToReplace.txt' -e "//d" InputFile.txt

I tried this command, it is not giving any error but it is not able to find the pattern
Code:
   Parameters "CParameters"

and hence not replacing it with contents from TextToReplace.txt .

I tried to escape the double quotes which is part of my input pattern using \ , but no luck.
# 6  
Old 03-21-2013
Seems to work here, and I'm pretty sure those are standard sed commands.

Code:
$ cat InputFile.txt
before
Parameters "CParameters"
after

$ cat TextToReplace.txt
aaa
bbb

$ sed -e '/Parameters "CParameters"/r TextToReplace.txt' -e "//d" InputFile.txt
before
aaa
bbb
after

$ sed -e '/CParameters/r TextToReplace.txt' -e "//d" InputFile.txt
before
aaa
bbb
after

You don't need to \ escape the "CParameters" double quotes, because inside the single quotes. I avoid those confusing \ characters if possible.

Any chance there is a tab character in the pattern? Maybe there is something about the pattern that is "funny" and is confusing us. You might try to find the pattern with grep first, to figure out the regular expression. Another option is to experiment with the final version above with simple pattern, verify that works, then move on to the longer pattern.
This User Gave Thanks to hanson44 For This Post:
# 7  
Old 03-22-2013
Thanks a lot, it is working now. It was of great help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed read contents of file and print value another file

Trying to use sed to insert the contents of a file into the end of each line in another file file1 This is a line Here is another line This is yet another line Here is a fourth line file2 TEXT desired output This is a line TEXT Here is another line TEXT This is yet another... (6 Replies)
Discussion started by: jimmyf
6 Replies

2. Shell Programming and Scripting

Using sed's hold-space to filter file contents

I wrote an awk script to filter "uninteresting" commands from my ~/.bash_history (I know about HISTIGNORE, but I don't want to exclude these commands from my current session's history, I just want to avoid persisting them across sessions). The history file can contain multi-line entries with... (6 Replies)
Discussion started by: ivanbrennan
6 Replies

3. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Sed: how to use file contents in replacement string

I want to replace a string by contents of file. I am trying the following sed command: cat sample | sed "s^<enter description here>^`cat details`^" But it is not working. a=`cat details` and using $a will not help since it will affect the whitespaces. What am I missing in the above sed... (5 Replies)
Discussion started by: anand_bh
5 Replies

5. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

6. Shell Programming and Scripting

sed substitute command -- need help

I am trying to do what I thought should be a simple substitution, but I can't get it to work. File: Desire output: I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

using sed command to display contents where line numbers are stored in variables

if i want to display the contents of a file between say line number 3 and 10 then i use the following command sed -n '3,10p' filename if this 3 was contained in x and 10 was contained in y then how wud this command modified? sed -n '$x,$yp' filename does not work..please advise (2 Replies)
Discussion started by: arindamlive
2 Replies

8. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

9. Shell Programming and Scripting

Convert contents of file to lower case with SED

Hi I what to add option to existing sed code to convert target file to lower case #!/bin/ksh SOURCE_DATA_DEST=/ora TARGET_DATA_DEST=/home/oracle/alexz TARGET_DB_SID=T102_test sed -e "s/REUSE/SET/g" \ -e "s/NORESETLOGS/RESETLOGS/g" \ T102_ccf.sql > target.sql Thanks (2 Replies)
Discussion started by: zam
2 Replies

10. Shell Programming and Scripting

SED To insert Directory Contents to file

I am on a HP-UX machine I have a directory called "/u01/blobs" and the files look like this: ls -1 7398 7399 7400 I need to produce a comma delimited file with the following format: filename,location/filename i.e: 7398,/u01/blobs/7398 7399,/u01/blobs/7399 7400,/u01/blobs/7400 What... (3 Replies)
Discussion started by: NomDeGuerre
3 Replies
Login or Register to Ask a Question