问题
I have a file whose first 28 rows are just words. Starting from 29th to 100th row, I have position information of atoms for A, B and C, and their 3d coordinates. Now what I would like to do is to change Z (the 4th column) in a way related to Y (the 3rd column) for Row 29-100:
Z = Z + sin(Y/10*Pi). Is that possible just in the terminal? Thanks.
A 0.016333 0.003203 0.472723
A 0.016333 0.035228 0.472723
B 0.016333 0.067253 0.472723
B 0.016333 0.099278 0.472723
C 0.016333 0.131303 0.472723
C 0.016333 0.163328 0.472723
回答1:
Perl solution:
perl -lane '$F[3] += sin($F[2]/10 * 4 * atan2 1, 1) if 29 .. 100;
print "@F"
' input_file > output_file
-n
reads the input line by line-a
splits each line on whitespace into the @F array-l
adds a newline toprint
4 * atan2 1, 1
is π- 29 .. 100 is true only for lines in the given range
来源:https://stackoverflow.com/questions/37514800/modify-text-column-based-on-the-column-before-it