2
sed -i '$a\curl -s http://whatismyip.org/' file

Trying to find a way to pull the WAN IP and insert it into the last line of a file as illustrated above (not working of course). This will be utilized via command line.

sed -i '$a\test' file

This will insert "test" after the last line in "file" as utlilized but how could I output the result of a function or command in it's place within Sed's syntax? Any suggests (awk, perl, bash script?) are welcome!

2 Answers 2

4

sed isn't required here. Just use this:

curl -s http://whatsmyip.org >> your.file

Note that bash supports the >> redirection operator which appends a program's output to a file

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

1 Comment

nice to see it helped you
3

hek2mgl has shown you how to solve this specific problem. To address the more general question, you can do:

var=$(some command line)

This sets the shell variable $var to the output of the command. Then you can subsitute this into sed with:

sed -i "\$a\\$var" file

2 Comments

Thanks for this answer! Just for clarification does \\ escape text input so a variable can be called within Sed parameters? Example: read -e -p "login: " -i $NewUser sed -i -e 's|Username = "username"|Username = "$NewUser"|g' loginfile.foo
\\ is an escaped slash, so that the slash doesn't escape the $ after it, which allows the shell to expand the variable inside the sed parameter.

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.