Score:1

appending variable to end of file with sed

so flag

I have dates on the last line in files like this:

20230220

and I want to add hours like this:

2023022000

The hours are continuously assigned to a variable i in a loop.

How to use sed inside the loop to append that variable to the end of files.

I tried with:

sed '$a|$i|>end>' file.txt

But couldn't append to file.

Raffa avatar
jp flag
Why `sed` ... Since you're using a shell loop already, append it with `echo "$i" >> file.txt`
CaosWT avatar
so flag
Yes i did it with echo but boss doesnt want like this ..
Score:1
jp flag

You can do it with sed like so:

sed '$a'"$i"'' file.txt

Notice: you need to break the single quotes ' around your shell parameter/variable so that it is expanded to its value or otherwise, it will be treated as literal $i.

You might, however not need sed at all ... Since you're already using a shell loop, you can append it using shell redirection with the append operator >> like so:

echo "$i" >> file.txt

That will put the value from the expansion of $i on a new line after the last line of the file.

sed, however might be needed if you want to put that value on the same last line at the end next to what's already in that line ... e.g. if the file has an existing last line that already has 20230220 at its end and you want to add 00 right next to it so it becomes 2023022000, then you can use:

sed '$s/$/'"$i"'/' file.txt

Notice: for sed to edit the files(instead of only printing to terminal), you need to set the -i flag like so:

sed -i '$a'"$i"'' file.txt
CaosWT avatar
so flag
It gives right result but variable still doesnt append to file
Raffa avatar
jp flag
@CelaleddinKızılkaya Then it might be a problem in assigning your variable at first place ... You can assign current hour for example with command substitution e.g. `i=$(date "+%H")` ... Plus, don't forget to break the single quotes as shown in the answer around the variable to expand to its value.
CaosWT avatar
so flag
I have a loop from 00 to 18 three by three i is assigned here
Raffa avatar
jp flag
@CelaleddinKızılkaya Please add your loop code to your question if you want me to look at it.
CaosWT avatar
so flag
Ok i added some part of my code
Raffa avatar
jp flag
Ah @CelaleddinKızılkaya … :-) … I think I got you now LOL … In order to edit the file itself instead of just printing to terminal, you need to set the `-i` flag i.e. like so `sed -i '$a'"$i"'' file.txt` … Is that what you want? … I am on my phone now but I’ll fix your question formatting when I get hold of a PC in a little while.
CaosWT avatar
so flag
Yes exactly it is thanks very much <3.
Raffa avatar
jp flag
@CelaleddinKızılkaya You’re welcome … I rolled back your edit to your question as the added part is not relevant anymore.
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.