Replace space in column with letter for several rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace space in column with letter for several rows
# 1  
Old 04-09-2016
Replace space in column with letter for several rows

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

Code:
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:

Code:
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.
# 2  
Old 04-09-2016
Try:
Code:
sed '/^ATOM /s/./A/22' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-09-2016
That worked perfectly. Very much appreciated!
# 4  
Old 04-10-2016
To make sure you replace nothing but a space, do it like
Code:
 sed '/^ATOM /s/ /A/22' file

. Scrutinizer's proposal will replace ANY character.
# 5  
Old 04-10-2016
Quote:
Originally Posted by RudiC
To make sure you replace nothing but a space, do it like
Code:
 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:
Code:
sed '/^\(ATOM ................\) /s//\1A/' file

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-10-2016
Which could be reduced to:
Code:
sed '/^ATOM /s/\(.\{21\}\) /\1A/' file

or
Code:
sed 's/^\(ATOM .\{16\}\) /\1A/' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace the first letter of each line by a capital

Hi, I need to replace, as the title says, the first letter of each line (when it's not a number) by the same letter, but capital. For instance : hello Who 123pass Would become : Hello Who 123pass Is there a way with sed to do that ? Or other unix command ? Thank you :) (7 Replies)
Discussion started by: ganon551
7 Replies

2. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

3. Shell Programming and Scripting

Replace rows to column

I have a csv file and i want to convert its rows into columns sample file like this Row1,1,2,3,......,n row2,4,5,6,.......,n . . . . rown,7,8,9,........,n i want it like this row1,row2,....,rown 1,4,.............,7 (4 Replies)
Discussion started by: sagar_1986
4 Replies

4. Shell Programming and Scripting

Replace a character of specified column(s) of all rows in a file

Hi - I have a file "file1" of below format. Its a comma seperated file. Note that each string is enclosed in double quotes. "abc","-0.15","10,000.00","IJK" "xyz","1,000.01","1,000,000.50","OPR" I want the result as: "abc","-0.15","10000.00","IJK" "xyz","1,000.01","1000000.50","OPR" I... (8 Replies)
Discussion started by: njny
8 Replies

5. Shell Programming and Scripting

Program to match the id and replace one letter in the content

Hi all, I have one file with a sequence and the other file which says the position and the letter to be changed. I have to match two files and replace content. Example is shown which will describe what I want to do. For example, file 1 has many sequences and few are shown below sequence file:... (6 Replies)
Discussion started by: kaav06
6 Replies

6. Shell Programming and Scripting

Transpose multiple rows (with a mix of space and enter) to a single column

How to change the uploaded weekly file data to the following format? New Well_Id,Old Well_Id,District,Thana,Date,Data,R.L,WellType,Lati.,Longi. BAG001,PT006,BARGUNA,AMTALI,1/2/1978,1.81,2.29,Piezometer,220825,901430 BAG001,PT006,BARGUNA,AMTALI,1/9/1978,1.87,2.29,Piezometer,220825,901430... (3 Replies)
Discussion started by: sara.nowreen
3 Replies

7. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

8. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

9. UNIX for Dummies Questions & Answers

Extracting rows from a space delimited text file based on the values of a column

I have a space delimited text file. I want to extract rows where the third column has 0 as a value and write those rows into a new space delimited text file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

10. UNIX for Dummies Questions & Answers

How to add a space after an Upper case letter

Hi I have two questions. 1. how to convert "EverythingIsFine" to "Everything Is Fine" in a txt file. 2. how to convert everything to upper case letter and reverse, I hope there is a general purpose script for this. (1 Reply)
Discussion started by: onthetopo
1 Replies
Login or Register to Ask a Question