-1

I am trying to create a shell script which can replace a text in another file.

open> gedit replace_curr_dt.sh

#!/bin/bash
curr_dt=$(date)
echo $curr_dt
sed 's/%CURR_DATE%/$curr_dt/g' curr_dt.txt

but it does not change the other file content.

  • Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – oguz ismail Jan 28 '20 at 08:17
  • 1
    In addition to using double quotes, you have to write the output of `sed` to a file, e.g. `sed "s/%CURR_DATE%/$curr_dt/g" curr_dt.txt > curr_dt.txt.new && mv curr_dt.txt.new curr_dt.txt` – Bodo Jan 28 '20 at 08:49
  • but its only replacing the text I want it to be replaced by current date which we obtain from date – Smitraj Raut Jan 28 '20 at 09:22

1 Answers1

-1

Use the -i flag to replace inline.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – HatLess Sep 03 '21 at 20:09
  • 1
    @HatLess: It _does_ offer an answer. [Please review](https://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue). It could use more explanation and, perhaps, an example of how to apply it. But that’s a reason to downvote, not delete. – Jeremy Caney Sep 04 '21 at 01:24
  • 1
    @JeremyCaney Noted . BTW, the downvote was not me, just the review. – HatLess Sep 04 '21 at 14:21