Sed: how to use file contents in replacement string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: how to use file contents in replacement string
# 1  
Old 09-01-2015
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:
Code:
cat sample | sed "s^<enter description here>^`cat details`^"

But it is not working.
Code:
a=`cat details`

and using $a will not help since it will affect the whitespaces.

What am I missing in the above sed command?

Last edited by Corona688; 09-01-2015 at 05:09 PM..
# 2  
Old 09-01-2015
Hello anand_bh,

Following are the points for your posts.
1st: Kindly use code tags as per forum rules while using codes/commands/inputs in your posts. You can go through the link for forum rules as follows.
https://www.unix.com/misc.php?do=cfrules

2nd: There is no need of using cat file | sed .... here. As sed can read file by itself.

3rd: Following is an example for uses for sed command.
Let's say we have an example input file named test2 where we want to substitute a string named Control with chumma, then we can use as follows.
Code:
 sed 's#Control#chumma#g' test2

Output will be as follows.
Code:
chumma|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241
chumma|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241

4th: For more information on sed you can read manual page entry for it by man sed or you can go through n number of example by search utility option in this forum itself.

Hope this helps.


Thanks,
R. Singh
# 3  
Old 09-01-2015
Thanks Ravinder for the reply.
Sorry for not following the code rule.

I will read the man page and update the result here if I found any.
# 4  
Old 09-01-2015
Moderator's Comments:
Mod Comment @Ravinder: Yes, it is perfectly OK to remind people to read the man pages. This point can never be stressed enough. On the other hand, "read the man page" or "search the web" without even so much as a pointer what to search for is not all that much helpful.

I might be naive, but many years of answering questions here still failed to make me jaded enough to suppose ill intentions every time. If someone writes a question i prima facie suppose s/he has already undergone some effort and came up blank - if only because s/he lacked the correct keywords to search for.

Many thanks for your consideration.


Quote:
Originally Posted by anand_bh
I will read the man page and update the result here if I found any.
You might want to concentrate on the "r" subcommand, which takes a file argument.

It will not be easy to directly replace a part of a line by the content of a file. To it seems like this is one of the very few occasions where you need to pipe the output of one sed into another. Consider the following:

inputfile:

Code:
===== line 1 =====
===== line 2 =====

replacementfile:

Code:
this
will
go
in

Script:
Code:
sed '1 r replacementfile'

Result:

Code:
===== line 1 =====
this
will
go
in
===== line 2 =====

The "r" subcommand takes a file parameter and places the contents of this file into the output stream. Notice, that this is NOT a replacement! The content of the file does NOT land on the pattern space and therefore you cannot further manipulate the result.

This is why i said you might need two seds, piping the output of the first (where the file content insertion takes place) into the other (where further manipulation might take place.

I hope this helps.

bakunin
# 5  
Old 09-01-2015
Actually, when I use the sed given in post#1, it works perfectly for me as is. What exactly goes wrong? Please post sample and details files!
# 6  
Old 09-02-2015
Yes it works.
Code:
 cat details
abc
 cat sample
some text
hello <enter description here> world
other text
 read a < details
 sed "s^<enter description here>^$a^" sample
some text
hello abc world
other text

q.e.d.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed, Inline replacement of string with spaces

Hello, Just surfed on the web for probable answers but could not get them working. I wish to replace the string containing spaces by another phrase but below answers did not work. My string is: PAIN & GAIN I wish to convert it to: P&G I just need it working with sed with function -i ... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Recursive replacement of search string using sed

Dear Unix Forum Group Members, Please do let me know how I can replace the double pipe with single pipe recursively on single record. Sample Input Data: DN set|Call prefix||| Called number address nature 0||| *789|||||||ALL number types 0||| 00||||||||ALL number types 10||... (5 Replies)
Discussion started by: srinu.kadem
5 Replies

3. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

4. 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

5. Shell Programming and Scripting

Use regex in replacement string in SED

Hi, I need to use the regex in the replacement string in SED command. something like sed -e ' s/\(^\{5\}\).\{150\}\(.*\)$/\10\{30\}1\{30\}A\{60\}B\{30\}\2/' abc which means for all the lines in file abc that starts with 5 characters, I need to replace character 6-151... (6 Replies)
Discussion started by: snowline84
6 Replies

6. UNIX for Dummies Questions & Answers

sed string replacement question

Hey everybody. I've got a simple problem but am unsure how to resolve it. I am using a script to edit multiple files at once. Inside the script I am using an sed command to make the changes. My problem is that I can only get it to work for stings that contain a word or words. How can I modify it to... (1 Reply)
Discussion started by: iwatk003
1 Replies

7. Shell Programming and Scripting

Exact String replacement with sed

Hi, What should be the syntax to match and replace an exact string using sed? And not replacing any string that contain the value? Eg. testtest etstetst testetst testtttt etsttest testtesttest testtest I only want to replace the line with exact string "testtest" with "123456" ... (2 Replies)
Discussion started by: srage
2 Replies

8. Shell Programming and Scripting

SED complex string replacement

sed -i 's:"ps -ef | grep $(cat $PID_FILE) | grep -v grep":"ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep":g' scriptName That's what I'm attempting to do. I'm attempting to replace this: ps -ef | grep $(cat $PID_FILE) | grep -v grep with this: ps -C java -o... (5 Replies)
Discussion started by: cbo0485
5 Replies

9. Shell Programming and Scripting

String replacement using sed

I need to search and replace a particular string in a file. Only the exact match of the string should be replaced. eg: File contents : abc abcd abcdef --> Replace only 'abc' with 'xyz', but it should not replace abcd with xyzd. So the o/p should be: xyz abcd abcdef. How can this be done? I... (5 Replies)
Discussion started by: sngk
5 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question