astha
astha

Reputation: 613

how to replace periodic occurrence of a array in a text file

I have below text file

      (      21.349659891       0.000000000       0.000000000 )
      (       0.000000000      21.349659891       0.000000000 )
      (       0.000000000       0.000000000      24.110175194 )


  Ex  (       -7.61882       -0.00000        0.00000 )
  Ey  (        0.00000       -7.61882        0.00000 )
  Ez  (        0.00000        0.00000       -7.04421 )
  Ex  (       -7.61882        0.00000        0.00000 )
  Ey  (       -0.00000       -7.61882        0.00000 )
  Ez  (        0.00000        0.00000       -7.04421 )
  Ex  (        3.46435        0.00000        0.00000 )
  Ey  (        0.00000        3.46435        0.00000 )
  Ez  (        0.00000        0.00000        2.88195 )
  Ex  (        3.46435        0.00000        0.00000 )
  Ey  (        0.00000        3.46435        0.00000 )
  Ez  (        0.00000        0.00000        2.88195 )
  Ex  (        4.13400        0.00000        0.00000 )
  Ey  (        0.00000        4.13400        0.00000 )
  Ez  (        0.00000        0.00000        4.12922 )
  Ex  (        4.13400        0.00000        0.00000 )
  Ey  (        0.00000        4.13400        0.00000 )
  Ez  (        0.00000        0.00000        4.12922 )

For rest of the files there is repetitive occurrence of Ex, Ey and Ez.

I want to update this text file as below (CLM1 deals top three lines while CLM2 is needed for rest of the text file):

CLM1(:,1)=21.349659891       0.000000000       0.000000000,
CLM1(:,2)=0.000000000      21.349659891       0.000000000,
CLM1(:,3)=0.000000000       0.000000000      24.110175194,
CLM2(:,1,1)=-7.61882       -0.00000        0.00000,
CLM2(:,2,1)= 0.00000       -7.61882        0.00000,
CLM2(:,3,1)= 0.00000        0.00000       -7.04421,
CLM2(:,1,2)=-7.61882        0.00000        0.00000,
CLM2(:,2,2)=-0.00000       -7.61882        0.00000,
CLM2(:,3,2)=0.00000        0.00000       -7.04421,
CLM2(:,1,3)=3.46435        0.00000        0.00000,
CLM2(:,2,3)=0.00000        3.46435        0.00000,
CLM2(:,3,3)=0.00000        0.00000        2.88195,
CLM2(:,1,4)=3.46435        0.00000        0.00000,
CLM2(:,2,4)=0.00000        3.46435        0.00000,
CLM2(:,3,4)=0.00000        0.00000        2.88195,
CLM2(:,1,5)=4.13400        0.00000        0.00000,
CLM2(:,2,5)=0.00000        4.13400        0.00000,
CLM2(:,3,5)=4.13400        0.00000        0.00000,
CLM2(:,1,6)=4.13400        0.00000        0.00000,
CLM2(:,2,6)=0.00000        4.13400        0.00000,
CLM2(:,3,6)=0.00000        0.00000        4.12922,

Notice that CLM2 data has periodicity as per Ex, Ey and Ez while next number (1 to 6) is occurring sequentially equal to the number of times that Ex or Ey or Ez appears (starting from 1). If Ex/Ey/Ez repeats upto 100 times then then this CLM2 will extend upto 99. In such case last three lines will be like this

CLM2(:,1,99)=
CLM2(:,2,99)=
CLM2(:,3,99)=

I am looking for a shell scrip that can do this job for me.

I tried to update the files using below script

 sed '/^$/d' text-file | tr -d '(' |   tr -d '(' |  sed 's/Ex/CLM2(:,1,1) = /g' | sed 's/Ey/CLM2(:,1,1) = /g' | sed 's/Ez/CLM2(:,1,1)  = /g' | sed 's/$/,/' | sed '1s/^/CLM1(:,1) = /g' |  sed '2s/^/CLM1(:,2) = /g' |  sed '3s/^/CLM1(:,3) = /g'

But this did not work as a loop over Ex,Ey and Ez. I got below while using a above command

CLM1(:,1) =        21.349659891       0.000000000       0.000000000 ,
CLM1(:,2) =        0.000000000      21.349659891       0.000000000 ,
CLM1(:,3) =        0.000000000       0.000000000      24.110175194 ,
      CLM2(:,1,1) =          -7.61882       -0.00000        0.00000 ,
      CLM2(:,1,1) =           0.00000       -7.61882        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000       -7.04421 ,
      CLM2(:,1,1) =          -7.61882        0.00000        0.00000 ,
      CLM2(:,1,1) =          -0.00000       -7.61882        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000       -7.04421 ,
      CLM2(:,1,1) =           3.46435        0.00000        0.00000 ,
      CLM2(:,1,1) =           0.00000        3.46435        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000        2.88195 ,
      CLM2(:,1,1) =           3.46435        0.00000        0.00000 ,
      CLM2(:,1,1) =           0.00000        3.46435        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000        2.88195 ,
      CLM2(:,1,1) =           4.13400        0.00000        0.00000 ,
      CLM2(:,1,1) =           0.00000        4.13400        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000        4.12922 ,
      CLM2(:,1,1) =           4.13400        0.00000        0.00000 ,
      CLM2(:,1,1) =           0.00000        4.13400        0.00000 ,
      CLM2(:,1,1)  =           0.00000        0.00000        4.12922 ,

Upvotes: 0

Views: 59

Answers (1)

Ed Morton
Ed Morton

Reputation: 204558

$ cat tst.awk
NF {
    if ( $1 == "(" ) {
        clm = "CLM1"
    }
    else {
        if ( seen[$1] || (clm == "CLM1") ) {
            clm = "CLM2"
            blockNr++
            lineNr = 0
            delete seen
        }
        seen[$1]++
        sub(/^[[:space:]]*[^[:space:]]+/,"")
    }

    gsub(/[[:space:]]*[()][[:space:]]*/,"")

    printf "%s(:,%d%s)=%s,\n", clm, ++lineNr, (blockNr ? "," blockNr : ""), $0
}

.

$ awk -f tst.awk file
CLM1(:,1)=21.349659891       0.000000000       0.000000000,
CLM1(:,2)=0.000000000      21.349659891       0.000000000,
CLM1(:,3)=0.000000000       0.000000000      24.110175194,
CLM2(:,1,1)=-7.61882       -0.00000        0.00000,
CLM2(:,2,1)=0.00000       -7.61882        0.00000,
CLM2(:,3,1)=0.00000        0.00000       -7.04421,
CLM2(:,1,2)=-7.61882        0.00000        0.00000,
CLM2(:,2,2)=-0.00000       -7.61882        0.00000,
CLM2(:,3,2)=0.00000        0.00000       -7.04421,
CLM2(:,1,3)=3.46435        0.00000        0.00000,
CLM2(:,2,3)=0.00000        3.46435        0.00000,
CLM2(:,3,3)=0.00000        0.00000        2.88195,
CLM2(:,1,4)=3.46435        0.00000        0.00000,
CLM2(:,2,4)=0.00000        3.46435        0.00000,
CLM2(:,3,4)=0.00000        0.00000        2.88195,
CLM2(:,1,5)=4.13400        0.00000        0.00000,
CLM2(:,2,5)=0.00000        4.13400        0.00000,
CLM2(:,3,5)=0.00000        0.00000        4.12922,
CLM2(:,1,6)=4.13400        0.00000        0.00000,
CLM2(:,2,6)=0.00000        4.13400        0.00000,
CLM2(:,3,6)=0.00000        0.00000        4.12922,

Upvotes: 2

Related Questions