0

I want to prepend the data of a file (random.txt) into another set of files (real.txt) in a folder.

Take the following example:

$ cat random
some text

$ cat real.txt
sample text

If I run the following command:

$ sed i.old '1s;^;some text\n;' real.txt

$ cat real.txt
some text

sample text

This command prepends the text of file random into file real.txt, but the content of random is not fixed. Is it possible to redirect the content of the random into real.txt?

I tried the following command:

$ sed -i.old "1s;^;$(cat random)\n;" real.txt

$ cat real.txt

Which gives the output:

some text
sample text

The problem occurs when i my random fil gets a txt starting with "/*". It shows following error:

$ cat real.txt
/*new text
 new line*/
$sed -i.old "1s;^;$(cat random)\n;" real.txt
 sed: -e expression #1, char 16: unterminated `s' command
DTdev
  • 139
  • 10
  • 2
    It's a little unclear what your goal is. Can you show the actual file and the change you expect? Edit your question to include this. – Panki Sep 26 '19 at 17:37
  • changed it a little. Hope it is more clear now – DTdev Sep 26 '19 at 17:50
  • 2
    What I'm getting here is you're trying to do cat random real.txt? And just have the text from random before real.txt? – Panki Sep 26 '19 at 17:54
  • correct. but i get following error when i try with cat: cat random real.txt > real.txt cat: real.txt: input file is output file – DTdev Sep 26 '19 at 17:57
  • 1
    PIggy backing off of your conversation with @Panki, you can just do cat random real.txt > tmp followed by mv tmp real.txt. You cannot pipe into an output file when it is also the input of the cat command. – Jason K Lai Sep 26 '19 at 18:10
  • 1
    Specifically, https://unix.stackexchange.com/a/56977/135943 – Wildcard Sep 26 '19 at 18:11
  • @DTdev Answer edited. – Jim L. Sep 26 '19 at 18:55

1 Answers1

1

Specifically addressing the question of why your sed syntax doesn't work, process substitution will not occur inside single quotes.

This syntax works for me:

sed -i.old "1s;^;$(cat random)\\
;" filename

If you're using GNU sed, this will work also:

sed -i.old "1s;^;$(cat random)\n;" filename

Addressing your edit, and focusing on GNU sed, two solutions come to mind:

1) avoid syntax inside random which bothers sed. In your specific case, that is the use of a literal newline inside the file.

Instead of:

/* line1 
   line2 */

Encode the newline (at the end of line 1) as \n:

/* line1\n   line2 */

The final newline in random (at the end of line 2) is already encoded as \n in the sed command line.

2) abandon in-place editing with sed and use a "cat-and-mv" approach:

for f in filename; do
    mv $f $f.old
    cat random $f.old > $f
done
Jim L.
  • 7,997
  • 1
  • 13
  • 27
  • ok. i got that it works, but if the text of my random contains "/*" i get following error: sed: -e expression #1, char 5: unterminated `s' command – DTdev Sep 26 '19 at 18:23
  • @DTdev Then you need to go back and edit your question to show your actual use case. It's possible sed might not be the correct tool for your job. – Jim L. Sep 26 '19 at 18:35