# Paste a string after another string in file

**URL:** <https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [February 20, 2012, 4:01am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983 "2012-02-20T04:01:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FelipeAd](https://community.unix.com/user_avatar/community.unix.com/felipead/32/2154_2.png) [@FelipeAd](https://community.unix.com/u/FelipeAd)\
**Post date:** [February 20, 2012, 4:01am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/1 "2012-02-20T04:01:19Z")

</div>

Hello,  
how can I paste a string1 after a particular string2 in a file?

---

<div class="post-metadata">

**Author:** ![rangarasan](https://community.unix.com/user_avatar/community.unix.com/rangarasan/32/2356_2.png) [@rangarasan](https://community.unix.com/u/rangarasan)\
**Post date:** [February 20, 2012, 4:03am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/2 "2012-02-20T04:03:16Z")

</div>

Hi,

can you provide sample input and expected output?  
can you elabrate your requirement ?

Cheers,  
Ranga:)

---

<div class="post-metadata">

**Author:** ![FelipeAd](https://community.unix.com/user_avatar/community.unix.com/felipead/32/2154_2.png) [@FelipeAd](https://community.unix.com/u/FelipeAd)\
**Post date:** [February 20, 2012, 4:06am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/3 "2012-02-20T04:06:28Z")

</div>

I have a file in the form:

```nohighlight
title1:
title2:
title3:
title4:

```

I want to paste the string "name" after title2. That will be:

```nohighlight
title1
title2: name
title3
title4

```

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [February 20, 2012, 4:07am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/4 "2012-02-20T04:07:46Z")

</div>

```nohighlight
sed "s/title2:/& name/" inputfile

```

---

<div class="post-metadata">

**Author:** ![rangarasan](https://community.unix.com/user_avatar/community.unix.com/rangarasan/32/2356_2.png) [@rangarasan](https://community.unix.com/u/rangarasan)\
**Post date:** [February 20, 2012, 4:13am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/5 "2012-02-20T04:13:46Z")

</div>

where can i get the name, is it constant one or else it may be dynamic value ?

I assume that name is a constant one that wont get anywhere from the file.

```nohighlight
 
1. sed 's/^\(title2:\)/\1 name/g' Input_File
2. awk '{if( $0 ~ /^title2:/ ){print $0,"name";}else{print $0;}}' Input_File
 

```

Cheers,  
Ranga:)

---

<div class="post-metadata">

**Author:** ![FelipeAd](https://community.unix.com/user_avatar/community.unix.com/felipead/32/2154_2.png) [@FelipeAd](https://community.unix.com/u/FelipeAd)\
**Post date:** [February 20, 2012, 4:18am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/6 "2012-02-20T04:18:19Z")

</div>

Thank you all for your replies.

rangarasan, you are right, "name" is a dynamic value. In fact I have multiple 'title' files and a file with a list of 'names'. My actual code would be to read each line from the 'name' list and insert this after "title2" in each 'title' file.

---

<div class="post-metadata">

**Author:** ![rangarasan](https://community.unix.com/user_avatar/community.unix.com/rangarasan/32/2356_2.png) [@rangarasan](https://community.unix.com/u/rangarasan)\
**Post date:** [February 20, 2012, 4:22am UTC](https://community.unix.com/t/paste-a-string-after-another-string-in-file/304983/7 "2012-02-20T04:22:56Z")

</div>

okay 🙂
