Score:0

End of line in sed command

et flag

I need to append some variable string at the end of every line in the file using the sed command.

Let's say that $var is a variable string.

If I use a single quote sed -i 's/$/$var/' inputfile.txt --> then $var gets printed at the end of the line.
If I use double quote sed -i "s/$/$var/" inputfile.txt --> then it cannot resolve "$" as the end of the line.

How can I use End of line variable and double quote in single one-line sed command?

pa4080 avatar
cn flag
Hello. The syntax of the substitution command in `sed` is `s/<searched>/<replacement>/<flags>`. You can change the delimiter to some other symbol, and you can omit the `<flags>` part, but you **cannot omit the third delimiter** (the third slash `/` in this case). In addition in your badly formatted second example you have one double quote mark and one single quote mark... So please correct all these typos, and make sure they are correct within your terminal commands...
vipin kumar soni avatar
et flag
I have edited my Query, it was typo here. But still even with that, not seeing the intended results.
Score:3
ao flag

There are several ways to solve your problem. The easiest in this case is to double-quote the sed command: sed -i "s/$/$var/" inputfile.txt. The first $ is not followed by anything that could be a valid variable name, and will be passed to sed as a plain $ character, meaning end of line. The expression $var, on the contrary, will, when between double quotes, be expanded to the value of the variable var; if that value contains slash characters /, you will want to use another separator than / for sed's s command, for instance a = like sed -i "s=$=$var=" inputfile.txt.

In other cases it may be easier to split the command in parts that need variable expansion, between double quotes, and parts that do not want this, between single quotes. You can mix them as, for example, sed -i 's/some search string$/some replacement string'"$var"'some more/' inputfile.txt. The shell will stitch these differently quoted strings together seamlessly, if you put no spaces between them. But that is difficult to read; in many cases escaping single characters is easier to read, and less to type.

vipin kumar soni avatar
et flag
sed -i "s/$/$var/" inputfile.txt is printing the string $var not the value stored in $var.
db-inf avatar
ao flag
I tested it in a bash shell before posting. Which shell do you use?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.