Array manipulation with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array manipulation with awk?
# 1  
Old 07-13-2015
Array manipulation with awk?

Dear friends,

I'm wondering if we could do some simple math on two arrays with the same size?
a1
Code:
 Fe                -0.21886700   -0.01417600   -0.24390300
 C                  2.20529400    0.89434100   -0.61061000
 C                 -1.89657700   -0.74793000   -0.07778200
 C                 -0.78045500    1.73233800   -0.62423900
 C                  0.65734600   -1.61607700   -0.60938700
 C                  0.11361900    0.12951700    1.54504000
 O                  3.16544300    0.60457600   -0.07949200
 O                 -2.93978200   -1.20649900    0.03749900
 O                 -1.17744800    2.79042100   -0.79151800
 O                  1.13104100   -2.64621800   -0.76292400
 O                  0.30764300    0.20965100    2.67185100

a2
Code:
 Fe                 0.15952600    0.01711400   -0.24029800
 C                 -1.97261300   -0.93404400   -0.72618200
 C                  1.78269300    0.85491000   -0.07347200
 C                  0.90251400   -1.66591000   -0.61391800
 C                 -0.76286300    1.60188200   -0.56505400
 C                 -0.16654700   -0.17442800    1.53653200
 O                 -2.98122600   -0.88421900   -0.18555400
 O                  2.79651400    1.38892100    0.03302700
 O                  1.43942200   -2.67322900   -0.73876500
 O                 -1.24544800    2.64138200   -0.66125100
 O                 -0.36511000   -0.29028300    2.66508200

math:
a3i=a1+i/10(a2-a1) , where i=0,1,2,3,4,5,6,7,8,9,10;
if i=0, a3=a1; if i=10, a3=a2; we also will get other nine kinds of a3 on the basis of the above function.

Thank you very much for your kind help!

Best wishes,
Zhen
# 2  
Old 07-13-2015
Why don't you clearly slow the output you desire and also, show what you tried?
# 3  
Old 07-13-2015
Quote:
Originally Posted by senhia83
Why don't you clearly slow the output you desire and also, show what you tried?
because I have no idea how to do this use awk , bash, or python, or fortran.
I know it would be not so hard, but I just tried and I have no clue on this problem.
The main function is to use a script to get the difference of a2 and a1;
# 4  
Old 07-13-2015
WHAT? That description leaves a wide field for speculations! However, with wildest guessing I came up with
Code:
paste file1 file2 | awk '{print $1, $2+(NR-1)/10*($6-$2), $3+(NR-1)/10*($7-$3), $4+(NR-1)/10*($8-$4)}' OFS="\t" OFMT="%12.8f"
Fe    -0.21886700     -0.01417600     -0.24390300
C      1.78750330      0.71150250     -0.62216720
C     -1.16072300     -0.42736200     -0.07692000
C     -0.27556430      0.71286360     -0.62114270
C      0.08926240     -0.32889340     -0.59165380
C     -0.02646400     -0.02245550      1.54078600
O     -0.52255840     -0.28870100     -0.14312920
O      1.07562520      0.61029500      0.03436860
O      0.91604800     -1.58049900     -0.74931560
O     -1.00779910      2.11262200     -0.67141830
O     -0.36511000     -0.29028300      2.66508200

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-13-2015
Quote:
Originally Posted by RudiC
WHAT? That description leaves a wide field for speculations! However, with wildest guessing I came up with
Code:
paste file1 file2 | awk '{print $1, $2+(NR-1)/10*($6-$2), $3+(NR-1)/10*($7-$3), $4+(NR-1)/10*($8-$4)}' OFS="\t" OFMT="%12.8f"
Fe    -0.21886700     -0.01417600     -0.24390300
C      1.78750330      0.71150250     -0.62216720
C     -1.16072300     -0.42736200     -0.07692000
C     -0.27556430      0.71286360     -0.62114270
C      0.08926240     -0.32889340     -0.59165380
C     -0.02646400     -0.02245550      1.54078600
O     -0.52255840     -0.28870100     -0.14312920
O      1.07562520      0.61029500      0.03436860
O      0.91604800     -1.58049900     -0.74931560
O     -1.00779910      2.11262200     -0.67141830
O     -0.36511000     -0.29028300      2.66508200

Hi Rudic, you are a genius and always write a script with mysterious awk to do the magic things! Where is i? I need to get 11 points from a1 to a2 including a1 when i=0 and a2 when i=1 and other nine coordinates when i=1,2,3,4,5,6,7,8,9;
I know it would be better to do with fortran or python. But I just wondering that awk could also do this trick.

---------- Post updated at 12:23 AM ---------- Previous update was at 12:15 AM ----------

Quote:
Originally Posted by RudiC
WHAT? That description leaves a wide field for speculations! However, with wildest guessing I came up with
Code:
paste file1 file2 | awk '{print $1, $2+(NR-1)/10*($6-$2), $3+(NR-1)/10*($7-$3), $4+(NR-1)/10*($8-$4)}' OFS="\t" OFMT="%12.8f"
Fe    -0.21886700     -0.01417600     -0.24390300
C      1.78750330      0.71150250     -0.62216720
C     -1.16072300     -0.42736200     -0.07692000
C     -0.27556430      0.71286360     -0.62114270
C      0.08926240     -0.32889340     -0.59165380
C     -0.02646400     -0.02245550      1.54078600
O     -0.52255840     -0.28870100     -0.14312920
O      1.07562520      0.61029500      0.03436860
O      0.91604800     -1.58049900     -0.74931560
O     -1.00779910      2.11262200     -0.67141830
O     -0.36511000     -0.29028300      2.66508200

GREAT! with your ideas, we can simply get the expected output with
Code:
for ((i=0;i<=10;i++)); do paste m1 m2 | awk -v num=$i '{print $1, $2+ num/10*($6-$2), $3+ num/10*($7-$3), $4+ num/10*($8-$4)}' OFS="\t" OFMT="%12.8f"; done

# 6  
Old 07-13-2015
I used NR-1, the record (or line) number, for i . As I said, as your spec was less than vague, I (as everybody else, I guess) was unable to infer what your expected output is. Are you sure you want 121 lines (11 iterations times 11 data sets) of output?
# 7  
Old 07-13-2015
Quote:
Originally Posted by RudiC
I used NR-1, the record (or line) number, for i . As I said, as your spec was less than vague, I (as everybody else, I guess) was unable to infer what your expected output is. Are you sure you want 121 lines (11 iterations times 11 data sets) of output?
Sorry for my ambiguous expression. Actually, I want 11 outputs. Each of these outputs will be written to a new file. I could do this within the for loop to prepare 11 outputs. I never know awk could do the math in Columns, which you posted in this thread. This idea is the most important part to solve the problem. Thank you so much in helping me for so many cases!

---------- Post updated at 01:36 AM ---------- Previous update was at 01:32 AM ----------

I thought this problem could only be solved with a multiple lines script. But you did it in a single line! what a surprise!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk manipulation

Hello all, Can someone help me with write part of code in awk to merge 2 files? Go through file1 check if number from column 3 exist in file2(column 2) if yes take value from column 1 and add to column 4 in file1. If value in column 4 exist in file1 skip it. file1... (2 Replies)
Discussion started by: vikus
2 Replies

2. Shell Programming and Scripting

awk manipulation

hello I have example file AA 11 BB 22 CC 33 And what I expect to have -a AA=11 -a BB=22 -a CC=33 can anyone help how I have this using awk? (1 Reply)
Discussion started by: vikus
1 Replies

3. Shell Programming and Scripting

awk manipulation

Hallo Family, I have csv file which has over a million records in it. All i want to do is to change field 2 to have the same value as field 10. sample file:Now 0860093239,Anonymous,unconditional,+27381230283,Anonymous,unconditional,y,public,,2965511477:0A Desired output: ... (2 Replies)
Discussion started by: kekanap
2 Replies

4. Shell Programming and Scripting

String manipulation using awk

I have the following string 512m512m I'm trying to split the string using awk awk '{ split(512m512m,a,"m") print $a; $a }' Sometimes the string could be 1024g1024g or 2048G2048G or 512M1024G how can i change the fieldsep to be a alphabet irrespective of case, and also... (8 Replies)
Discussion started by: ramky79
8 Replies

5. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

6. Shell Programming and Scripting

Bash array manipulation

seeking assistance on comparing two arrays using bash: array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5) array2=(disk2, disk5 vol2, vol4 ) 1) if two arrays have same elements; EXIT else populate array3 & array4 with elements that are different between array1 & array2 as:... (3 Replies)
Discussion started by: solaix14
3 Replies

7. Shell Programming and Scripting

Array manipulation in perl

hi all, i am trying to append the output of a find command (for different paths)in an array as below... my $res_array; $i=0; $dir="/orn/ops/regs"; foreach $block("am","xb"){ $bdir="$dir/$block"; $res_array=`find $bdir ! -user mainuser -printf \"\%u \%h\\n\"`; $i++; } i... (6 Replies)
Discussion started by: saapa
6 Replies

8. Shell Programming and Scripting

$0 manipulation in awk

OK, so if $0 represent the entire record... can I change $2 and will that be reflected back in $0? I think the following answers that YES, it does work. But is there anything I should be thinking about prior to doing this? What I am actually doing is part of 5 pages of scripting and awk... (1 Reply)
Discussion started by: joeyg
1 Replies

9. Shell Programming and Scripting

File manipulation with awk

Could you please help me to achieve the below: In a file I need to convert the multiple lines whose filed 1 and field 5 values are same into a single line but with the field 4 values comma separed as mentioned below. Fileds after 5 shall be discarded. Also here by default all other remaining... (6 Replies)
Discussion started by: dhams
6 Replies

10. Shell Programming and Scripting

awk manipulation

Hi , what a wonderful command but so hard to maintain ! i have a file like that : 03/07/2006 05:58:45 03/07/2006 06:58:45 03/07/2006 07:58:50 03/07/2006 08:58:50 and i want to read it and keep only the lines with 3rd field less than 07:00:00 writing it in a second file ! ... (2 Replies)
Discussion started by: Nicol
2 Replies
Login or Register to Ask a Question