Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

I would like to print a string with a variable formula using awk in multiple rows

$
0
0

I have inherited a script which takes a file and extracts some data from it, creates a new file and based on this data writes a formula based on the data extracted. The data extracted varies in count on each run however this data is consistent is where it is placed as is the formula used.

At the moment the script writes the formula and as it is hard coded the cell references remain the same for each row. I would like for this formula to be written to reference the row it is in rather than what is currently hard-coded now which is referencing the second row - the first line of the formula which is correct.

At the moment the below prints the formula for all rows - I want this to be dynamic and reference the actually row the formula is in.

awk -F"," 'NR==1 {
   for (i=1;i<=NF;i++) {
      if ($i=="Primary") {
         Primary=i;
      }
      if ($i=="Sub Type") {
         Sub_Type=i;
      }   
      if ($i=="METHOD") {
         METHOD=i;
      }
      if ($i=="GLOBAL") {
         GLOBAL=i;
      }
     if ($i=="UNIQUE") {
         UNIQUE=i;
      }   
      if ($i=="ID") {
         ID=i;
      }
      if ($i=="PRIVATE_PLACEMENT") {
         PRIVATE_PLACEMENT=i;
      }
   }
}
NR > 1 {print $METHOD "," $Sub_Type "," $Primary "," $GLOBAL "," $ID "," $UNIQUE "," "\"\=abc\(D2\&\"\" xxxx\"\"\,\"\"SECURITY\"\"\"\)" "," "\"\=xyz\(D2\&\"\" xxxx\"\"\,\"\"last\"\"\,\"\"01\/01\/1900\"\"\,Today\(\)\,\"\"Quote\"\"\,\"\"P\"\"\,\"\"Points\=1\"\"\,\"\"cols\=2\;rows\=1\"\")\"" "," "," "," "\"\=xyz\(E2\&\"\" xxxx\"\"\,\"\"last\"\"\,\"\"01\/01\/1900\"\"\,Today\(\)\,\"\"Quote\"\"\,\"\"P\"\"\,\"\"Points\=1\"\"\,\"\"cols\=2\;rows\=1\"\")\"" "," "," "," "\"\=xyz\(F2\&\"\" buid\"\"\,\"\"last\"\"\,\"\"01\/01\/1900\"\"\,Today\(\)\,\"\"QuoteType\"\"\,\"\"P\"\"\,\"\"Points\=1\"\"\,\"\"cols\=2\;rows\=1\"\")\"" "," "," "," "\"\=abc\(D2\&\"\" xxxx\"\"\,\"\"source\"\"\"\)" "," "\"\=abc\(D2\&\"\" xxxx\"\"\,\"\"CRNCY\"\"\"\)""," "\"\=abc\(D2\&\"\" xxxx\"\"\,\"\"CRNCY\"\"\"\)" "," "\"\=IF\(ISBLANK\(H2\)\,\"\"\"\"\,IF\(ISBLANK\(K2\)\,\"\"\"\"\,IF\(H2\=K2\,\"\"Match\"\"\,\"\"No Match\"\"\"\)\)\)" ","  "\"\=IF\(ISBLANK\(H2\)\,\"\"\"\"\,IF\(ISBLANK\(N2\)\,\"\"\"\"\,IF\(H2\=N2\,\"\"Match\"\"\,\"\"No Match\"\"\"\)\)\)" "," "\"\=IF\(ISBLANK\(K2\)\,\"\"\"\"\,IF\(ISBLANK\(N2\)\,\"\"\"\"\,IF\(K2\=N2\,\"\"Match\"\"\,\"\"No Match\"\"\"\)\)\)" "," "," "=C2" "," "" "," "=H2" "," "=I2" "," "=Q2" "," "=R2"}' $WORK_DIR/sortedmissing.csv > $WORK_DIR/output1.xls

What would the best way to do this? Is there a function to drag the formula from the 7th column - the last column down to the last line in the file? Or is there a loop function that could do this for me?

Input file is a .xlsx file which is opened and some data is extracted from here and put into a new file - this can be arbitrary.

Method  Primary Sub Type    GLOBAL  ID  UNIQUE
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz
    123456789^DEFAULT^EOD^2019-07-12    Missing abcdefj 1234567 xyzxyz

output will look like this in csv - the first line is the headers and then under that is 5 columns of data copied from the input file - column 6 and across the script creates a number of formulas which are repeated in every row with the same cell reference however I want to make this reference the row it is in D2 in row 3 should be D3 and so on......

Method  Sub Type    Primary GLOBAL  ID  UNIQUE SECURITY_TYP FIGI LAST_UPDATE_DT 
Missing Price   123456^XXXXX^XXXXXEOD^2019-07-12 1234567890 1234567 123456789   =xxx(D2&" figi","SECURITY_TYP") =xxx(D2&" figi","last","01/01/1900",BToday(),"QuoteType","P","Points=1") I  
Missing Price 123456^XXXXX^XXXXXEOD^2019-07-12  1234567890  1234567 123456789   =BDP(D2&" figi","SECURITY_TYP") =xxxx(D2&" figi","last","01/01/1900",BToday(),"QuoteType","P","Points=1") 

Output in true form

Method,Sub Type,Primary,GLOBAL,ID,UNIQUE,SECURITY_TYP,FIGI LAST_UPDATE_DT,FIGI PX_LAST,,SEDOL LAST_UPDATE_DT,SEDOL PX_LAST,,BB_UNIQUE LAST_UPDATE_DT,BB_UNIQUE PX_LAST,,PRICING_SOURCE,CRNCY,QUOTED_CRNCY,FIGI SEDOL PRICE CHECK,FIGI PRICE CHECK,SEDOL PRICE CHECK,,PrimaryIdentifier,CL_SUBSCRIBER,PRICE_DATE,BPL_SERVICE_PRICE,BPL_PRICING_SOURCE,PRICE_CRNCY
,Missing Price,123456789^DEFAULT^EOD^2019-07-19,abcdefj,123456,xyzxyzxyz,"=abc(D2&"" moon"",""TYP"""),"=abc(D2&"" moon"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(E2&"" star"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(F2&"" buid"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(D2&"" moon"",""pricing_source"""),"=abc(D2&"" moon"",""CRNCY"""),"=abc(D2&"" moon"",""QUOTED_CRNCY"""),"=IF(ISBLANK(H2),"""",IF(ISBLANK(K2),"""",IF(H2=K2,""Match"",""No Match"""))),"=IF(ISBLANK(H2),"""",IF(ISBLANK(N2),"""",IF(H2=N2,""Match"",""No Match"""))),"=IF(ISBLANK(K2),"""",IF(ISBLANK(N2),"""",IF(K2=N2,""Match"",""No Match"""))),,=C2,,=H2,=I2,=Q2,=R2
I,Missing Price,123456789^DEFAULT^EOD^2019-07-19,abcdefj,,123456,"=abc(D2&"" moon"",""TYP"""),"=abc(D2&"" moon"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(E2&"" star"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(F2&"" buid"",""last"",""01/01/1900"",BToday(),""QuoteType"",""P"",""Points=1"",""cols=2;rows=1"")",,,"=abc(D2&"" moon"",""pricing_source"""),"=abc(D2&"" moon"",""CRNCY"""),"=abc(D2&"" moon"",""QUOTED_CRNCY"""),"=IF(ISBLANK(H2),"""",IF(ISBLANK(K2),"""",IF(H2=K2,""Match"",""No Match"""))),"=IF(ISBLANK(H2),"""",IF(ISBLANK(N2),"""",IF(H2=N2,""Match"",""No Match"""))),"=IF(ISBLANK(K2),"""",IF(ISBLANK(N2),"""",IF(K2=N2,""Match"",""No Match"""))),,=C2,,=H2,=I2,=Q2,=R2

What you can see here is that the code above writes the line with the formula in two rows but uses the same cell references "D2" - I would like the second row to reference D3 etc

Thanks in advance


Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>