# Replace space in column with letter for several rows

**URL:** <https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [April 9, 2016, 9:24am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657 "2016-04-09T09:24:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Egy](https://community.unix.com/letter_avatar/egy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Egy](https://community.unix.com/u/Egy)\
**Post date:** [April 9, 2016, 9:24am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/1 "2016-04-09T09:24:43Z")

</div>

I have a pbd file, which has the following format:

```nohighlight
TITLE Protein X
MODEL 1
ATOM 1 N PRO 24 45.220 71.410 43.810 1.00 0.00            
ATOM 2 H1 PRO 24 45.800 71.310 42.000 1.00 0.00            
TER
ENDMDL

```

Column 22 is the chain identifier for lines that begin with ATOM. I would like to replace the space in column 22 with the letter A. The actual file has thousands of rows labeled ATOM. An example output is given below:

```nohighlight
TITLE Protein X
MODEL 1
ATOM 1 N PRO A 24 45.220 71.410 43.810 1.00 0.00            
ATOM 2 H1 PRO A 24 45.800 71.310 42.000 1.00 0.00            
TER
ENDMDL

```

Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 9, 2016, 10:08am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/2 "2016-04-09T10:08:58Z")

</div>

Try:

```nohighlight
sed '/^ATOM /s/./A/22' file

```

---

<div class="post-metadata">

**Author:** ![Egy](https://community.unix.com/letter_avatar/egy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Egy](https://community.unix.com/u/Egy)\
**Post date:** [April 9, 2016, 10:17am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/3 "2016-04-09T10:17:15Z")

</div>

That worked perfectly. Very much appreciated!

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 10, 2016, 1:50am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/4 "2016-04-10T01:50:53Z")

</div>

To make sure you replace nothing but a space, do it like

```nohighlight
 sed '/^ATOM /s/ /A/22' file 

```

. Scrutinizer's proposal will replace ANY character.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 10, 2016, 3:24am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/5 "2016-04-10T03:24:52Z")

</div>

> [@rudic](#):
>
> To make sure you replace nothing but a space, do it like
> 
> ```plaintext
> sed '/^ATOM /s/ /A/22' file 
> 
> ```
> 
> . Scrutinizer's proposal will replace ANY character.

The above code will change the 22nd \<space\> character on a line; not a space that is the 22nd character on a line. If the requirement is to change the 22nd character on a line to an \<A\> if and only if that character was a \<space\>, try something more like (assuming you have a POSIX-conforming version of `sed` :

```plaintext
sed '/^\(ATOM ................\) /s//\1A/' file

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 10, 2016, 3:48am UTC](https://community.unix.com/t/replace-space-in-column-with-letter-for-several-rows/359657/6 "2016-04-10T03:48:52Z")

</div>

Which could be reduced to:

```nohighlight
sed '/^ATOM /s/\(.\{21\}\) /\1A/' file

```

or

```nohighlight
sed 's/^\(ATOM .\{16\}\) /\1A/' file

```
