Sponsored Content
Top Forums Shell Programming and Scripting Sed: how to use file contents in replacement string Post 302953770 by bakunin on Tuesday 1st of September 2015 08:47:53 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 12:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy