Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

linux - Modify text column based on the column before it

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 to print
  • 4 * atan2 1, 1 is π
  • 29 .. 100 is true only for lines in the given range

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...