# String replacement

**URL:** <https://community.unix.com/t/string-replacement/228479>\
**Category:** Shell Programming and Scripting\
**Created:** [January 21, 2009, 8:26am UTC](https://community.unix.com/t/string-replacement/228479 "2009-01-21T08:26:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![reddybs](https://community.unix.com/letter_avatar/reddybs/32/6_22734026db12803ebb3e059622ac3534.png) [@reddybs](https://community.unix.com/u/reddybs)\
**Post date:** [January 21, 2009, 8:26am UTC](https://community.unix.com/t/string-replacement/228479/1 "2009-01-21T08:26:38Z")

</div>

Hi I am new to shell scripting but i manage to do some simple things.  
I am trying to replace a string in one file. I am using sed to replace but it is not permanently writing to the file, rather it is temporary. I want to know whether is there any another method to replace a string in a file permenantly.

# THIS IS WHAT I HAVE DONE:

$ cat conf.txt  
This is first line which contains NO  
This is second line which contains NO  
This is thrid line which contains NO

I used sed to replace NO with YES in 2nd line.

$cat -n conf.txt | grep 2 | sed 's/NO/YES/g'

2 This is second line which contains YES

But i want this to happen in the file. I want the replacement to be happened in file(2nd line though).

Please guide me regarding the same and correct me if iam wrong. ☹

Thanks in advance.  
ss

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [January 21, 2009, 9:02am UTC](https://community.unix.com/t/string-replacement/228479/2 "2009-01-21T09:02:01Z")

</div>

With awk:

```nohighlight
awk 'NR==2{gsub("NO","YES")}1' file

```

Regards

---

<div class="post-metadata">

**Author:** ![reddybs](https://community.unix.com/letter_avatar/reddybs/32/6_22734026db12803ebb3e059622ac3534.png) [@reddybs](https://community.unix.com/u/reddybs)\
**Post date:** [January 21, 2009, 9:12am UTC](https://community.unix.com/t/string-replacement/228479/3 "2009-01-21T09:12:38Z")

</div>

> [@franklin52](#):
>
> With awk:
> 
> ```plaintext
> awk 'NR==2{gsub("NO","YES")}1' file
> 
> ```
> 
> Regards

Thank you very much for the help.

But the file is not getting updated with the change.

Say for example, if the command is run, the file should be changed with the output it produces.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [January 21, 2009, 9:54am UTC](https://community.unix.com/t/string-replacement/228479/4 "2009-01-21T09:54:50Z")

</div>

```nohighlight
awk 'NR==2{gsub("NO","YES")}1' file > newfile && mv newfile file

```

Regards

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [January 21, 2009, 10:23am UTC](https://community.unix.com/t/string-replacement/228479/5 "2009-01-21T10:23:50Z")

</div>

```nohighlight
sed -e '2s/NO/YES/' file > newfile && mv newfile file

```

---

<div class="post-metadata">

**Author:** ![reddybs](https://community.unix.com/letter_avatar/reddybs/32/6_22734026db12803ebb3e059622ac3534.png) [@reddybs](https://community.unix.com/u/reddybs)\
**Post date:** [January 21, 2009, 10:46am UTC](https://community.unix.com/t/string-replacement/228479/6 "2009-01-21T10:46:05Z")

</div>

> [@franklin52](#):
>
> ```plaintext
> awk 'NR==2{gsub("NO","YES")}1' file > newfile && mv newfile file
> 
> ```
> 
> Regards

Thats great its working fine.  
I have one more doubt... If i want to change the line number dynamically, ex: using some variable which gets input from user.

awk 'NR=="$i"{gsub("NO","YES")}1' conf.txt

Very sorry if im silly...

Thankf for you help.🙂

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [January 21, 2009, 11:16am UTC](https://community.unix.com/t/string-replacement/228479/7 "2009-01-21T11:16:05Z")

</div>

```nohighlight
awk -v lin=${userInputLine} 'NR==lin {gsub("NO","YES")}1' conf.txt

```

---

<div class="post-metadata">

**Author:** ![reddybs](https://community.unix.com/letter_avatar/reddybs/32/6_22734026db12803ebb3e059622ac3534.png) [@reddybs](https://community.unix.com/u/reddybs)\
**Post date:** [January 22, 2009, 7:50am UTC](https://community.unix.com/t/string-replacement/228479/8 "2009-01-22T07:50:48Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk -v lin=${userInputLine} 'NR==lin {gsub("NO","YES")}1' conf.txt
> 
> ```

Thank you... Its working....  
Cheers:)
