# How to insert a new string after a text in a .txt document using bash?

**URL:** <https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914>\
**Category:** Linux on Laptops\
**Created:** [May 28, 2024, 3:57am UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914 "2024-05-28T03:57:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShintY](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/shinty/32/1384_2.png) [@ShintY](https://community.learnlinux.tv/u/ShintY)\
**Post date:** [May 28, 2024, 3:57am UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914/1 "2024-05-28T03:57:50Z")

</div>

I want to insert a particular string of text in an already existing text document using a bash command line. Let us say for example i have a text document named test.txt and contains the following text -

```auto
hi everyone
my name is 

```

i want to update the test.txt document in real time with same name to get this -

```auto
Hi everyone 
my name is 
Shinty

```

I have tried using sed but for some reason it cant update the .txt with the same name. I also used sed with -i tag but i get an empty document after executing the code.

Any help would be appreciated,  
Thank you

---

<div class="post-metadata">

**Author:** ![hypoiodous](https://community.learnlinux.tv/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@hypoiodous](https://community.learnlinux.tv/u/hypoiodous)\
**Post date:** [May 28, 2024, 7:39am UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914/2 "2024-05-28T07:39:06Z")

</div>

The easiest way I can think of is using shell redirection.

```auto
echo 'Shinty' >> test.txt

```

A single \> sign redirects the output and overwrites the contents of the file, but double \>\> signs appends to it instead.

---

<div class="post-metadata">

**Author:** ![ShintY](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/shinty/32/1384_2.png) [@ShintY](https://community.learnlinux.tv/u/ShintY)\
**Post date:** [May 28, 2024, 9:41am UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914/3 "2024-05-28T09:41:15Z")

</div>

hmm i should have phrased my question properly, i dont want it to append but rather be between 2 strings. Is that possible?

---

<div class="post-metadata">

**Author:** ![ThatGuyB](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/thatguyb/32/784_2.png) [@ThatGuyB](https://community.learnlinux.tv/u/ThatGuyB)\
**Post date:** [May 28, 2024, 1:08pm UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914/4 "2024-05-28T13:08:15Z")

</div>

I assume you meant

```auto
hi everyone
is my name

```

and you want to put your name in between those lines. You can do it in a number of ways, but it’ll all be dependent on either the contents or the line.

I don’t like using `sed -i`, because it’s what I call bashism (technically gnu-ism and it’s not compatible with BSD `sed`), so I always use temporary files.

```auto
sed 's/everyone/everyone\nShinty/' testfile.txt > testfile.tmp.txt
mv testfile.tmp.txt testfile.txt

```

In bashism, you just remove the output redirect and the tmp file with the -i flag in sed.

```auto
sed -i 's/everyone/everyone\nShinty/' testfile.txt

```

If you know you want to put the name on line 2, then you don’t have to look for the word “everyone” and replace it, you can just sed number.

```auto
sed '2i Shinty' testfile.txt > testfile.tmp.txt
mv testfile.tmp.txt testfile.txt

```

(I don’t remember if the 2i works on BSDs, I don’t think I tried that and I don’t have a bsd box running right now to check).

---

<div class="post-metadata">

**Author:** ![ShintY](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/shinty/32/1384_2.png) [@ShintY](https://community.learnlinux.tv/u/ShintY)\
**Post date:** [May 28, 2024, 9:03pm UTC](https://community.learnlinux.tv/t/how-to-insert-a-new-string-after-a-text-in-a-txt-document-using-bash/3914/6 "2024-05-28T21:03:45Z")

</div>

Thanks a lot this was very helpful to me, much appreciate your help 🙂
