Hi, I am new to Linux, so I have a basic question. I want to convert a file like this:
ATOM 1 O SOL 2 20.662 13.241 18.640 1.00
ATOM ...
Enjoy an ad free experience by logging in. Not a member yet?
Register.
-
Script for replacing columns
Hi, I am new to Linux, so I have a basic question. I want to convert a file like this:
ATOM 1 O SOL 2 20.662 13.241 18.640 1.00
ATOM 2 H SOL 2 21.639 13.282 18.849 1.00
ATOM 3 C SOL 2 20.497 13.637 17.737 1.00
ATOM 4 O SOL 3 0.535 11.254 21.810 1.00
ATOM 5 H SOL 3 1.097 12.079 21.746 1.00
ATOM 6 C SOL 3 -0.370 11.436 21.426 1.00
Into this:
ATOM 1 O SOL 2 20.662 13.241 18.640 4
ATOM 2 H SOL 2 21.639 13.282 18.849 2
ATOM 3 C SOL 2 20.497 13.637 17.737 3
ATOM 4 O SOL 3 0.535 11.254 21.810 4
ATOM 5 H SOL 3 1.097 12.079 21.746 2
ATOM 6 C SOL 3 -0.370 11.436 21.426 3
So basically, if the value at column "3" is O, H, or C, then I want to replace column "9" with 4, 2, or 3, respectively. I know I have to use the gawk or awk command.
Thank you for your help!
-

Originally Posted by
Tlaloc86
Hi, I am new to Linux, so I have a basic question. I want to convert a file like this:
ATOM 1 O SOL 2 20.662 13.241 18.640 1.00
ATOM 2 H SOL 2 21.639 13.282 18.849 1.00
ATOM 3 C SOL 2 20.497 13.637 17.737 1.00
ATOM 4 O SOL 3 0.535 11.254 21.810 1.00
ATOM 5 H SOL 3 1.097 12.079 21.746 1.00
ATOM 6 C SOL 3 -0.370 11.436 21.426 1.00
Into this:
ATOM 1 O SOL 2 20.662 13.241 18.640 4
ATOM 2 H SOL 2 21.639 13.282 18.849 2
ATOM 3 C SOL 2 20.497 13.637 17.737 3
ATOM 4 O SOL 3 0.535 11.254 21.810 4
ATOM 5 H SOL 3 1.097 12.079 21.746 2
ATOM 6 C SOL 3 -0.370 11.436 21.426 3
So basically, if the value at column "3" is O, H, or C, then I want to replace column "9" with 4, 2, or 3, respectively. I know I have to use the gawk or awk command.
Thank you for your help!
This would be more straightforward with R, but with auk this should do it:
[]$ awk '($3=="O"){x=4}($3=="H"){x=2}($3=="C"){x=3}{for(i= 1;i<=8;i++) printf "%s ", $i; print x;next}; {print $3,"not found"}' oldfile >newfile
Note the sanity check for bum records.
-
Thank you pkleiber 
This is really good information.
-

Originally Posted by
Tlaloc86
Hi, I am new to Linux, so I have a basic question. I want to convert a file like this:
ATOM 1 O SOL 2 20.662 13.241 18.640 1.00
ATOM 2 H SOL 2 21.639 13.282 18.849 1.00
ATOM 3 C SOL 2 20.497 13.637 17.737 1.00
ATOM 4 O SOL 3 0.535 11.254 21.810 1.00
ATOM 5 H SOL 3 1.097 12.079 21.746 1.00
ATOM 6 C SOL 3 -0.370 11.436 21.426 1.00
Into this:
ATOM 1 O SOL 2 20.662 13.241 18.640 4
ATOM 2 H SOL 2 21.639 13.282 18.849 2
ATOM 3 C SOL 2 20.497 13.637 17.737 3
ATOM 4 O SOL 3 0.535 11.254 21.810 4
ATOM 5 H SOL 3 1.097 12.079 21.746 2
ATOM 6 C SOL 3 -0.370 11.436 21.426 3
So basically, if the value at column "3" is O, H, or C, then I want to replace column "9" with 4, 2, or 3, respectively. I know I have to use the gawk or awk command.
Thank you for your help!
Here is a "filter" written only in bash that will do what you want.
Code:
#!/bin/bash
#
while read line; do
field=( ${line} )
case "${field[2]}" in
O) field[8]=4 ;;
H) field[8]=2 ;;
C) field[8]=3 ;;
esac
echo "${field[@]}"
done
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules