Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

shell - text manipulation by addition and multiplication

I have a text file saved in the name 'test_file' that contain 6 rows and 7 columns as given below

 0.00 5.8 2.0  5.0 6.0 8.0 0.0
  10.00 5.8 2.0  1.0 1.0 1.2 9.6
  10.00 9.3 2.2  2.0 1.4 2.5 9.6
  30.00 9.3 2.2  1.2 1.5 1.9 1.4
  30.00 9.3 2.2  3.2 2.4 1.2 4.1
  60.00 9.8 3.5  1.4 2.7 3.2 4.5

I want to do some text manipulation in second and third column.

In the third column first two rows values should be same (2.0 and 2.0) and next three rows values are just the 0.2 increment of second row value(2.0+0.2=2.2,2.0+0.2=2.2,2.0+0.2=2.2).However, i don't want to change last row i want to keep it as it is.

After that in the second column, first two rows values should be just the multiplication of, first two rows of third column with 2.9. similarly next three rows of second column are just the multiplication of next three rows of third column with 4.227

other columns values i don't want to change at all.

Now i want to change the first two rows value of third column sequentially, 2.1,2.2....2.5 followed by same increment and multiplication.

For example when i change first two rows values of third column from original 2.0 to 2.1 then the expected output should be

0.00 6.09 2.1  5.0 6.0 8.0 0.0
  10.00 6.09 2.1  1.0 1.0 1.2 9.6
  10.00 9.722 2.3  2.0 1.4 2.5 9.6
  30.00 9.722 2.3  1.2 1.5 1.9 1.4
  30.00 9.722 2.3  3.2 2.4 1.2 4.1
  60.00 9.8 3.5  1.4 2.7 3.2 4.5

and i want to save the output file in different name such as file2.1.txt....file2.5.txt

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

awk to the rescue!

$ awk 'p {print p} 
         {pp=$0; v=$3; $3+=0.1; $2*=$3/v; p=$0} 
     END {print pp}' file | column -t

0.00   6.09     2.1  5.0  6.0  8.0  0.0
10.00  6.09     2.1  1.0  1.0  1.2  9.6
10.00  9.72273  2.3  2.0  1.4  2.5  9.6
30.00  9.72273  2.3  1.2  1.5  1.9  1.4
30.00  9.72273  2.3  3.2  2.4  1.2  4.1
60.00  9.8      3.5  1.4  2.7  3.2  4.5

since you want special treatment for the last record, delay processing using the previous record p, also you want unmodified last record, so store the original previous record in pp and print at the END. Delayed printing will print the modified records and last one will be unmodified.

You can specify number formatting as well but I didn't think it was important...

To run for multiple increments, just add an outer loop

 $ for inc in {1..5}; 
   do awk -v inc=$inc '...
                       ... $3+=(inc/10) ...
                       ...' file > file."$inc".txt

   done

You can pass the increment (actually 10 times the increment) to awk script as a variable, use in in the script as well as in the output filename. The only change in the awk script is the increment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...