How to Use `sed` for Inserting Text with Variables in Bash?

0
0
Asked By CuriousCoder42 On

Hey everyone! I'm diving into bash scripting and working on an install script for my NixOS setup. I need to replace a comment in my file with some Nix code from an external text file using `sed`. The tricky part is that I'm trying to use double quotes `""` for evaluating bash variables and the `s` option at the start seems to require single quotes `''`. From what I've gathered, I might have to switch between using single and double quotes while writing my sed expression. Can anyone help me figure out how to do this correctly? Here's what I've tried: `sudo sed -i 's|#Install new host hook|'"$(< /etc/nixos/scripts/helperFiles/newHostFlakeBlock.txt)"'|' /etc/nixos/flake.nix.

4 Answers

Answered By BashBuff On

Hey, just to clarify, the statement about the `s` option only working with single quotes isn’t accurate. It works with double quotes too; those quotes don't actually get seen by `sed` since they’re manipulated by the shell, not the program itself. So feel free to use double quotes wherever necessary!

Answered By ScriptingSage On

For sure, if your inserted file has multiple lines, using `sed` to replace could get a bit messy; it might result in an unterminated `s/.../.../` command. You might want to check out some references to help clear things up:
1. [Grymoire Sed Guide](https://www.grymoire.com/Unix/Sed.html#uh-22)
2. [Unix Stack Exchange](https://unix.stackexchange.com/questions/141387/sed-replace-string-with-file-contents)
These links should help you grasp it better!

HelpfulHelper21 -

Thanks for these! I'll check them out for sure.

Answered By TechWhiz99 On

To simplify things, you can ask `sed` to append the text from your file right after the matched line instead of trying to replace it. Here’s a quick example of how you can do that:
```bash
sed -i '/#Install new host hook/{r /etc/nixos/scripts/helperFiles/newHostFlakeBlock.txt
d;}' /etc/nixos/flake.nix
```
Just ensure the path to your file follows a newline after the `r` command. If you cram everything into one line, it’ll choke on it!

BashMaster12 -

Got it! What about using double quotes around that? Would that cause problems?

Answered By SimplifyItNow On

Honestly, you may be overcomplicating this. You can just use a simple read command and replace without relying heavily on `sed`. Try this:
```bash
IFS= read -rd '' < /etc/nixos/flake.nix
printf -- %s "
${REPLY/#Install new host hook/$(< /etc/nixos/scripts/helperFiles/newHostFlakeBlock.txt)}"
```
This way, you can directly redirect the output to your file without bothering with `sed`. Let me know if that works!

CuriousCoder42 -

I'll try this if I can't get my previous method to work, thanks!

AwesomeCoder77 -

It actually worked! Is this a common practice for inserting larger text? Or is it more of a niche approach?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.