2

http://paste.ubuntu.com/24184546/

This is the file. Here, I need to replace the 6th column with numbers from 1 to 158 respectively.

The output should be like, http://paste.ubuntu.com/24184581/

How do I do it, using linux commands? Thanks in advance.

2
  • Can you explain how exactly you want it to be replaced ? Mar 15, 2017 at 20:04
  • I have added the sample output.
    – User
    Mar 15, 2017 at 20:07

1 Answer 1

2

What you request is basically incrementing a counter whenever column 4 changes. The awk command below does exactly that.

$ awk '$4 != last{count++};{$6=count;last=$4};1' input.txt                                     
ATOM 1149 N MET B 1 56.923 131.264 45.113 1.00140.13 N
ATOM 1150 CA MET B 1 58.169 131.437 44.333 1.00140.13 C
ATOM 1151 C MET B 1 58.225 130.444 43.220 1.00140.13 C
ATOM 1152 O MET B 1 58.569 130.785 42.089 1.00140.13 O
ATOM 1153 CB MET B 1 59.401 131.239 45.231 1.00140.13 C
ATOM 1154 CG MET B 1 59.536 129.826 45.799 1.00140.13 C
ATOM 1155 SD MET B 1 58.315 129.397 47.077 1.00140.13 S
ATOM 1156 CE MET B 1 59.098 130.370 48.397 1.00140.13 C
ATOM 1157 N SER B 2 57.860 129.183 43.517 1.00 86.33 N
ATOM 1158 CA SER B 2 57.898 128.164 42.517 1.00 86.33 C
ATOM 1159 C SER B 2 56.887 128.522 41.486 1.00 86.33 C
ATOM 1160 O SER B 2 55.901 129.195 41.780 1.00 86.33 O
ATOM 1161 CB SER B 2 57.559 126.761 43.047 1.00 86.33 C
ATOM 1162 OG SER B 2 56.212 126.721 43.495 1.00 86.33 O
ATOM 1163 N ALA B 3 57.122 128.084 40.237 1.00 45.56 N
ATOM 1164 CA ALA B 3 56.217 128.412 39.183 1.00 45.56 C
ATOM 1165 C ALA B 3 55.730 127.143 38.568 1.00 45.56 C
ATOM 1166 O ALA B 3 56.393 126.109 38.624 1.00 45.56 O
ATOM 1167 CB ALA B 3 56.865 129.235 38.059 1.00 45.56 C

Theory of operation

Each awk command can be structured in this way:

awk '/Condition/ {code block to run on condition};'

If condition is missing, code block runs for any line, regardless of its contents. In this example, we check whether the $4 field (column) has changed ( $4 != last , reads as field 4 not equals to the variable last ). If condition is true (which will be each time it changes and on the first line, when last isn't set yet), we increment the count variable.

The next code block {$6=count;last=$4} will run regardless of what line we're on. We just assign contents of count variable to field 6, and record what we saw in field 4. The count variable will come into existence once the script encounters first line of the text and will be created when command evaluates $4 != last part. last, on the other hand, will come to existence when code evaluates {$6=count;last=$4} part.

Finally 1 at the end is simply a trick to make condition to always evaluate to "true" and print. Just a shorthand instead of {print} block.

To save the result to a file, use > shell operator to redirect output in shell. For example

awk '$4 != last{count++};{$6=count;last=$4};1' input.txt > output.txt
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .