# Replacing text between two square brackets

**URL:** <https://community.unix.com/t/replacing-text-between-two-square-brackets/278399>\
**Category:** Shell Programming and Scripting\
**Created:** [December 2, 2010, 11:46am UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399 "2010-12-02T11:46:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaff](https://community.unix.com/letter_avatar/zaff/32/6_449c533f586939b6e5f246632a46421f.png) [@zaff](https://community.unix.com/u/zaff)\
**Post date:** [December 2, 2010, 11:46am UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/1 "2010-12-02T11:46:25Z")

</div>

hi guys,  
i'm writing a script that looks for a unquie id in a file and replaces a string between two square brackets on the same line as the unquie id:

```nohighlight
.......
.......
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
.......
.......

```

how can i find line with id 0001 and then replace only the text between the brackets?

Thanks in advance,  
Zaff

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [December 2, 2010, 11:53am UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/2 "2010-12-02T11:53:54Z")

</div>

```nohighlight
# echo "0001 zz 43242 [replace this text] name" | sed '/^0001/s/\[.*\]/anything/'
0001 zz 43242 anything name

```

```nohighlight
# cat input
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0001 zz 43242 [replace this text] name
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0001 zz 43242 [replace this text] name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country

# sed '/^0001/s/\[.*\]/anything/' input
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0001 zz 43242 anything name
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0001 zz 43242 anything name
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country
0002 sd 65466 [UK] country

#

```

---

<div class="post-metadata">

**Author:** ![zaff](https://community.unix.com/letter_avatar/zaff/32/6_449c533f586939b6e5f246632a46421f.png) [@zaff](https://community.unix.com/u/zaff)\
**Post date:** [December 2, 2010, 12:01pm UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/3 "2010-12-02T12:01:03Z")

</div>

...for further clarity:  
what i'm trying to do is replace the string between the brackets with its MD5 value. I'm basically anonymising the data. I can only search the file using the id, as i have no idea what the string-to-be-replaced is.

Regards,  
Zaff

---------- Post updated at 04:58 PM ---------- Previous update was at 04:55 PM ----------

Thanks for the quick response!  
.......and how about if i wanted to keep the square brackets?

Thanks,  
Zaff

---------- Post updated at 05:01 PM ---------- Previous update was at 04:58 PM ----------

...sorry, one more thing:  
how can i assign the value between the brackets to a variable?

Thanks,  
Zaff

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [December 2, 2010, 2:36pm UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/4 "2010-12-02T14:36:49Z")

</div>

Please give more clue :  
what is your input ?  
what is your expected output ?  
What do you want to do with the MD5 (log it into a file ? sometyhing else ?)

---

<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:** [December 2, 2010, 2:51pm UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/5 "2010-12-02T14:51:21Z")

</div>

> [@zaff](#):
>
> ...for further clarity:  
> what i'm trying to do is replace the string between the brackets with its MD5 value. I'm basically anonymising the data. I can only search the file using the id, as i have no idea what the string-to-be-replaced is.
> 
> .......and how about if i wanted to keep the square brackets?

```plaintext
MD5="New Text" 

sed '/0001/ s/\[.*]/['"$MD5]"'/' file

```

> [@zaff](#):
>
> how can i assign the value between the brackets to a variable?

```plaintext
var=$(sed -n '/0001/ s/.*\[\(.*\)].*/\1/p' file)

```

---

<div class="post-metadata">

**Author:** ![zaff](https://community.unix.com/letter_avatar/zaff/32/6_449c533f586939b6e5f246632a46421f.png) [@zaff](https://community.unix.com/u/zaff)\
**Post date:** [December 2, 2010, 3:01pm UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/6 "2010-12-02T15:01:13Z")

</div>

basically all i need is the following:

1. command similar to one u provided, but assigning the string in the brackets to a variable. i will then use this variable to generate an md5 (this i can do).

2. replace the content of the brackets with the md5. This command u have already kindly provided:

```nohighlight
echo "0001 zz 43242 [replace this text] name" |sed '/^0001/s/\[.*\]/my_md5/'

```

........only thing is, i need the brackets to remain as they are and not be removed along with the original string.

Regards,  
Zaff

---

<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:** [December 3, 2010, 8:03am UTC](https://community.unix.com/t/replacing-text-between-two-square-brackets/278399/7 "2010-12-03T08:03:14Z")

</div>

> [@zaff](#):
>
> 1. command similar to one u provided, but assigning the string in the brackets to a variable. i will then use this variable to generate an md5 (this i can do).

```plaintext
var=$(sed -n '/0001/ s/.*\[\(.*\)].*/\1/p' file)

```

> [@zaff](#):
>
> 1. replace the content of the brackets with the md5. This command u have already kindly provided

```plaintext
MD5="MD5 value"

sed '/0001/ s/\[.*]/['"$MD5]"'/' file

```
