2

I would like to replace string with file.txt content.

mtn="John"
fs=`cat file.txt`
lgtxt=`cat large_text.txt`

stxt1=`echo $lgtxt | sed "s/zzzz/$mtn/g"`
stxt2=`echo $stxt1 | sed "s/pppp/$fs/g"`

It replace 'zzzz' with value of 'mnt' but doesn't 'pppp'. File file.txt contain list of names eg: Tom jones Ted Baker Linda Evans in separate lines. I want to place them in file large_text.txt in separate lines like they are in oryginal file and separated by commas.

5
  • m4(1) is your friend. Commented Jun 25, 2015 at 18:15
  • Does pppp occur only once on a separate line? In that case we can look for another solution splitting the lgtxt by the pppp line. Or does pppp occur on some lines if [ "${line}" = "pppp" ]; then ... Commented Jun 25, 2015 at 19:11
  • No it occur within sentence and not only once. Commented Jun 25, 2015 at 19:24
  • SHow some sample input and expected output so we don't have to guess. Commented Jun 25, 2015 at 20:22
  • cat file.txt code Tom Jones Ted Baker Linda Evans cat large_file.txt This is the list of our students pppp. pppp are students of our class. Expected output: This is the list of our students Tom jones Ted Baker Linda Evans. Tom jones Ted Baker Linda Evans are students of our class. Commented Jun 29, 2015 at 18:44

1 Answer 1

7

You don't want to use a variable for the substitution (because it may well contain newlines, for example). I'm assuming that it's GNU sed given it's . In which case, see whether GNU sed's r command could help you:

`r FILENAME'
     As a GNU extension, this command accepts two addresses.

     Queue the contents of FILENAME to be read and inserted into the
     output stream at the end of the current cycle, or when the next
     input line is read.  Note that if FILENAME cannot be read, it is
     treated as if it were an empty file, without any error indication.

     As a GNU `sed' extension, the special value `/dev/stdin' is
     supported for the file name, which reads the contents of the
     standard input.

If pppp is on a line of its own, you could go with something like

/pppp/{
r file.txt
d
}

or, alternatively, the s command with e modifier:

`e'
     This command allows one to pipe input from a shell command into
     pattern space.  If a substitution was made, the command that is
     found in pattern space is executed and pattern space is replaced
     with its output.  A trailing newline is suppressed; results are
     undefined if the command to be executed contains a NUL character.
     This is a GNU `sed' extension.

This would look something like

s/pppp/cat file.txt/e

and is what you'll need if pppp is mid-line. Also, if you need to do further processing on file.txt, you could replace cat with whatever you need (though you need to be careful about quoting / and \).

A final option is to consider Perl, which will accept something very similar to your shell commands.

Sign up to request clarification or add additional context in comments.

2 Comments

code sed -i '/pppp/r file.txt' large_text.txt code nearly solved the problem ;-) . to get perfect result i need to delete 'pppp' after replacing. What about same situation but replaced text need to be separated by commas?
@Marcin: I've edited to address your followup comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.