AWK - Adding new line based on text in previous line? - awk

So I have some database table info in a file that looks like this:
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
Basically, I need it to look like this:
2.6 G 7.7 G qabc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G qabc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G qabc-def-ghi_2021-09-21_random_letters_random_numbers
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-wwwdef_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-wwwdef_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-wwwdef_2021-09-28_random_letters_random_numbers
55.1 K 165.3 K jkl-deghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-deghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-deghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-deghi-def_2021-09-26_random_letters_random_numbers
Where there is a new line after the start of each unique table-name prefix. Right now I'm having to do all of this manually for hundreds of table names. Also, if there is a way to count how many times each table name occurs, that would be great too.
Here is the code I got so far #Cyrus:
awk 'BEGIN{FS="[ _]+"} NR==1{last=$(NF-1)} NR>1 && last!=$(NF-1){printf RS} {last=$(NF-1); print}' test2.txt
Here is the output
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
This command works for table names like these:
2.6 G 7.7 G abc-def-ghi_2021-09-19
2.6 G 7.7 G abc-def-ghi_2021-09-20
2.6 G 7.8 G abc-def-ghi_2021-09-21
18.9 G 56.8 G def-abc-def_2021-09-21
110.3 M 331.0 M ghi-abc-def_2021-09-21
110.3 M 331.0 M ghi-abc-def_2021-09-27
110.4 M 331.2 M ghi-abc-def_2021-09-28
55.1 K 165.3 K jkl-ghi-def_2021-09-20
50.7 K 152.1 K jkl-ghi-def_2021-09-24
49.6 K 148.8 K jkl-ghi-def_2021-09-25
48.6 K 138.8 K jkl-ghi-def_2021-09-26

Seems like you can just do:
awk 'last && $5 != last { print count; count=0 } {last = $5; count++ } 1' FS='[ _]*'
eg:
$ cat input
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
$ awk 'last && $5 != last { print count; count=0 } {last = $5; count++ } 1' FS='[ _]*' input
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
3
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
1
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
3
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers

$ awk -F'[[:space:]_]+' '(NR>1) && ($5 != prev){print ""} {print; prev=$5}' file
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
$ awk -F'[[:space:]_]+' '(NR>1) && ($5 != prev){print cnt; cnt=0} {print; prev=$5; cnt++} END{if (cnt) print cnt}' file
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
3
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
1
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
3
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
4
The if (cnt) in the END sections is just so you don't print a null string or zero if the input file is empty.

Assumptions:
all input lines have a total of 5 space-delimited fields that look like: #.# {G,K,M} #.# {G,K,M} <table-name-prefix>_YYYY-MM-DD_random_letters_random_numbers
input file has already been sorted by <table-name-prefix>
objective is to add a blank line to the output before we process a line with a new/different <table-name-prefix>
OP mentions keeping track of how many times each unique <table-name-prefix> is seen, but since there's no mention of what to do with said number I'll just print it on the new 'blank' line (for now)
expected output is to look like the 2nd block of data provided in the question (with proviso that <table-name-prefix> counts are placed in the 'blank' lines)
One awk idea:
awk '{ split($5,arr,"_") # split field #5 on "_" delimiter
tabname=arr[1] # grab table name
if (tabname != prevname && prevname != "" ) { # if this is a new table name then ...
printf "%s\n", count # print a new line with a count of the last table name and then ...
count=0
}
print # print current line
count++ # increment count for table name
prevname = tabname # keep track of previous table name
}
END { printf "%s\n", count } # flush last table name count to stdout
' test2.txt
This generates:
2.6 G 7.7 G abc-def-ghi_2021-09-19_random_letters_random_numbers
2.6 G 7.7 G abc-def-ghi_2021-09-20_random_letters_random_numbers
2.6 G 7.8 G abc-def-ghi_2021-09-21_random_letters_random_numbers
3
18.9 G 56.8 G def-abc-def_2021-09-21_random_letters_random_numbers
1
110.3 M 331.0 M ghi-abc-def_2021-09-21_random_letters_random_numbers
110.3 M 331.0 M ghi-abc-def_2021-09-27_random_letters_random_numbers
110.4 M 331.2 M ghi-abc-def_2021-09-28_random_letters_random_numbers
3
55.1 K 165.3 K jkl-ghi-def_2021-09-20_random_letters_random_numbers
50.7 K 152.1 K jkl-ghi-def_2021-09-24_random_letters_random_numbers
49.6 K 148.8 K jkl-ghi-def_2021-09-25_random_letters_random_numbers
48.6 K 138.8 K jkl-ghi-def_2021-09-26_random_letters_random_numbers
4

Related

Sed command issue

I have this file inside a mariaDB that looks like this
name callerid secret context type host
1000 Omar Al-Ani <1000> op1000DIR MANAGEMENT friend dynamic
1001 Ammar Zigderly <1001> 1001 MANAGEMENT peer dynamic
1002 Lubna COO Office <1002> 1002 ELdefault peer dynamic
i want to convert it using sed and awk to look like this format
[1000]
callerid=Omar Al-Ani <1000>
secret=op1000DIR
context=MANAGEMENT
type=friend
host=dynamic
[1001]
callerid=Ammar Zigderly <1001>
secret=1001
context=MANAGEMENT
type=peer
host=dynamic
[1002]
callerid=Lubna COO Office <1002>
secret=1002
context=ELdefault
type=peer
host=dynamic
This is the output of this command head -3 filename | od -c on the input file
0000000 n a m e \t c a l l e r i d \t s e
0000020 c r e t \t c o n t e x t \t t y p
0000040 e \t h o s t \n 1 0 0 0 \t O m a
0000060 r A l - A n i < 1 0 0 0 >
0000100 \t o p 1 0 0 0 D I R \t M A N A
0000120 G E M E N T \t f r i e n d \t d y
0000140 n a m i c \n 1 0 0 1 \t A m m
0000160 a r Z i g d e r l y < 1 0 0
0000200 1 > \t 1 0 0 1 \t M A N A G E
0000220 M E N T \t p e e r \t d y n a m i
0000240 c \n
0000243
Any idea would be helpfull !
I think awk is going to be a bit simpler and easier (?) to modify if requirements change:
awk -F'\t' '
BEGIN { labels[2]="callerid"
labels[3]="secret"
labels[4]="context"
labels[5]="type"
labels[6]="host"
}
FNR>1 { gsub(/ /,"",$1) # remove spaces from 1st column
printf "[%s]\n",$1
for (i=2;i<=6;i++)
printf "\t%s=%s\n", labels[i],$i
print ""
}
' names.dat
This generates:
[1000]
callerid=Omar Al-Ani <1000>
secret=op1000DIR
context=MANAGEMENT
type=friend
host=dynamic
[1001]
callerid=Ammar Zigderly <1001>
secret=1001
context=MANAGEMENT
type=peer
host=dynamic
[1002]
callerid=Lubna COO Office <1002>
secret=1002
context=ELdefault
type=peer
host=dynamic
assuming tab separated fields
$ awk -F'\t' 'NR==1 {split($0,h); next}
{print "[" $1 "]";
for(i=2;i<=NF;i++) print "\t" h[i] ":" $i}' file.tcv
[1000]
callerid:Omar Al-Ani <1000>
secret:op1000DIR
context:MANAGEMENT
type:friend
host:dynamic
[1001]
callerid:Ammar Zigderly <1001>
secret:1001
context:MANAGEMENT
type:peer
host:dynamic
[1002]
callerid:Lubna COO Office <1002>
secret:1002
context:ELdefault
type:peer
host:dynamic

I wrote an optimization model of a paper with imaginary data in AMPL but there is some problem in running in terms of one of constraints about Years

All you need explain in question title. The mentioned Optimization model belong to a paper with same content but I tried to write the code and analyze what happen exactly through that. I will add the codes to have access to data and find out what is the problem when you are running that. I added the three segment of AMPL code down. Please help me with this.
MOD:
set O;
set J;
set Q;
set R; #use R instead of K because we have K in line 11 as parameter
set T;
param D {q in Q, t in T};
param K {o in O, q in Q};
param C {q in Q, j in J};
param S {r in R, q in Q};
param H {r in R, q in Q};
param F {q in Q};
param G {q in Q};
param B {q in Q};
param Delta;
param Omega;
param I;
param A;
param M;
var X {o in O, q in Q, j in J, t in T} binary;
var Z {o in O, q in Q, t in T} binary;
var P {q in Q, t in T} binary;
var Y {q in Q, t in T} binary;
var z {q in Q, t in T} binary;
var n {q in Q, t in T} integer >=0;
var m {q in Q, t in T} integer >=0;
var N {q in Q, t in T} integer >=0;
var Teta {q in Q, t in T} integer >=0;
var a {r in R, q in Q, t in T} integer >=0;
var b {r in R, q in Q, t in T} integer >=0;
var u binary;
var v binary;
var w binary;
minimize OF: sum {q in Q, j in J, o in O, t in T} C[q, j] * D[q, t] * X[o, q, j, t]
+ sum {o in O, q in Q, t in T} K[o, q] * Z[o, q, t]
+ sum {q in Q , t in T} N[q, t] * Delta
+ sum {t in T, q in Q} Omega * Teta[q, t]
+ sum {t in T, r in R, q in Q} S[r, q] * a[r, q, t]
+ sum {t in T, r in R, q in Q} H[r, q] * b[r, q, t]
+ sum {t in T, q in Q} F[q] * z[q, t]
+ sum {t in T, q in Q} G[q] * Y[q, t]
+ sum {t in T, q in Q} B[q] * P[q, t]
+ I * v + A * u;
subject to c1{q in Q , t in T}: sum {o in O} Z[o, q, t] = D[q, t];
subject to c2{o in O , q in Q , t in T}: sum {j in J} X[o, q, j, t] >= Z[o, q, t];
subject to c3{q in Q , t in T}: n[q, t]= n[q, t-1] + sum {r in R} a[r, q, t-1] + N[q, t] - sum {r in R} a[q, r, t-1];
subject to c4{q in Q , t in T}: m[q, t]= m[q, t-1] + sum {r in R} b[r, q, t-1] + Teta[q, t] - sum {r in R} b[q, r, t-1];
subject to c5{o in O , q in Q , t in T}: Z[o, q, t]<= n[q, t];
subject to c6{q in Q , t in T}: sum {o in O} X[o, q, 1, t]<= m[q, t];
subject to c7{t in T, r in R}: sum {q in Q} a[q, r, t]<= sum{q in Q} n[q, t-1]+ a[r, r, t];
subject to c8{r in R , t in T}: sum {q in Q} b[q, r, t]<= sum{q in Q} m[q, t-1]+ b[r, r, t];
subject to c91{o in O , q in Q , t in T}: X[o, q, 2, t-1]<= X[o, q, 2, t];
subject to c92{o in O , q in Q , t in T}: X[o, q, 3, t-1]<= X[o, q, 3, t];
subject to c10{q in Q , t in T}: sum {o in O} X[o, q, 2, t]- sum {o in O} X[o, q, 2, t-1]<= z[q, t];
subject to c11{q in Q , t in T}: sum {o in O} X[o, q, 3, t]- sum {o in O} X[o, q, 3, t-1]<= P[q, t];
subject to c12{q in Q , t in T , j in J}: X[3, q, j, t]- X[3, q, j, t-1]<= Y[q, t];
subject to c13: sum {t in T, q in Q} Z[4, q, t]<= M * u;
subject to c14: sum {t in T, q in Q} Z[5, q, t]<= M * v;
subject to c15{o in O , q in Q , t in T}: X[o, q, 1, t]<= M * (w-1);
subject to c16{q in Q , t in T}: Z[3, q, t]<= M * w;
subject to c17{q in Q , t in T}: Z[2, q, t]<= X[2, q, 1, t];
DATA:
set O := Treat1 Treat2 Treat3 Treat4 Treat5;
set J := Dewater1 Dewater2 Dewater3;
set Q := ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven;
set R := ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven; #use R instead of K because we have K in line 15 as parameter
set T := Year1 Year2 Year3 Year4 Year5;
param D :
Year1 Year2 Year3 Year4 Year5 :=
ArcticBay 0 1 0 1 0
Arviat 1 0 0 1 1
CambridgeBay 1 0 0 1 1
CapeDorse 1 0 1 1 1
ClydeRiver 1 1 0 1 0
GjoaHaven 0 0 1 0 1 ;
param K :
ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven :=
Treat1 4051 4221 6477 5205 6443 4088
Treat2 6758 4012 4994 5437 6215 6267
Treat3 5162 4051 6755 6742 6589 4139
Treat4 6556 5094 5963 6043 5745 5428
Treat5 5289 6050 5144 4456 6474 4432 ;
param C :
Dewater1 Dewater2 Dewater3 :=
ArcticBay 56299 32825 11894
Arviat 5977 15550 5958
CambridgeBay 20654 28654 19782
CapeDorse 3073 35237 33822
ClydeRiver 19218 48466 34032
GjoaHaven 23650 23304 42540 ;
param S :
ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven :=
ArcticBay 0 1359 1480 1458 1728 849
Arviat 2863 0 1229 1873 2353 2731
CambridgeBay 1821 1972 0 1772 1680 2726
CapeDorse 2754 2219 1817 0 1266 2814
ClydeRiver 1216 2632 2527 2782 0 949
GjoaHaven 1229 1238 2605 1060 2673 0;
param H :
ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven :=
ArcticBay 0 2269 1497 1218 2290 2344
Arviat 1424 0 1123 2494 2107 1114
CambridgeBay 2452 2365 0 1924 1292 1722
CapeDorse 1799 2003 2071 0 1216 1150
ClydeRiver 1322 1097 1827 1259 0 1191
GjoaHaven 1163 1444 2085 2398 1308 0;
param F :=
ArcticBay 1981
Arviat 1845
CambridgeBay 1529
CapeDorse 1721
ClydeRiver 1778
GjoaHaven 1802;
param G :=
ArcticBay 8639
Arviat 8829
CambridgeBay 11737
CapeDorse 8167
ClydeRiver 8573
GjoaHaven 10890;
param B :=
ArcticBay 6427
Arviat 9508
CambridgeBay 6661
CapeDorse 8016
ClydeRiver 6196
GjoaHaven 8359;
param Delta := 15000;
param Omega := 12000;
param I := 20000;
param A := 17000;
param M := 100000000;
RUN:
reset;
option solver CPLEX;
model SM.mod;
data SM.dat;
solve;
display X, Z, P, Y, z, n, m, N, Teta, a, b, u, v, w>b.out;
You got an error on constraint collection c3 because you are trying to do aritmetic calculations with values of set T, when in fact set T contains only strings (Year1, Year2, etc).
For instance, you wrote n[q, t-1], where t is in T. But what is 'Year1'-1 is not a valid AMPL expression. Other constraint collections have the same problem.
You could simply change the data of set T to be 1 2 3 4 5. But then you would run into other problems, because 1-1 is 0, and n[q,0] does not exist. You have to work on your formulation.
View project on PIFOP

Adding dataframes that share the same columns, and expand one more dimesion

I want to sum two dataframes that share the same columns
df1=pd.DataFrame(np.random.randn(3,3),index=list("ABC"),columns=list("XYZ"))
df2=pd.DataFrame(np.random.randn(3,3),index=list("abc"),columns=list("XYZ"))
My desired result would be:
X Y Z
A a
A b
A c
....
C c
How can I achieve this?
I have tried the following but didnt get what I wanted.
df1.add(df2,axis="columns")
You can create MultiIndex in both DataFrames first by MultiIndex.from_product and then reindex for MultiIndex in both DataFrames:
np.random.seed(45)
df1=pd.DataFrame(np.random.randn(3,3),index=list("ABC"),columns=list("XYZ"))
df2=pd.DataFrame(np.random.randn(3,3),index=list("abc"),columns=list("XYZ"))
mux = pd.MultiIndex.from_product([df1.index, df2.index])
df1 = df1.reindex(mux, level=0)
df2 = df2.reindex(mux, level=1)
print (df1)
X Y Z
A a 0.026375 0.260322 -0.395146
b 0.026375 0.260322 -0.395146
c 0.026375 0.260322 -0.395146
B a -0.204301 -1.271633 -2.596879
b -0.204301 -1.271633 -2.596879
c -0.204301 -1.271633 -2.596879
C a 0.289681 -0.873305 0.394073
b 0.289681 -0.873305 0.394073
c 0.289681 -0.873305 0.394073
print (df2)
X Y Z
A a 0.935106 -0.015685 0.259596
b -1.473314 0.801927 -1.750752
c -0.495052 -1.008601 0.025244
B a 0.935106 -0.015685 0.259596
b -1.473314 0.801927 -1.750752
c -0.495052 -1.008601 0.025244
C a 0.935106 -0.015685 0.259596
b -1.473314 0.801927 -1.750752
c -0.495052 -1.008601 0.025244
df3 = df1.add(df2,axis="columns")
print (df3)
X Y Z
A a 0.961480 0.244637 -0.135550
b -1.446939 1.062248 -2.145898
c -0.468677 -0.748279 -0.369901
B a 0.730805 -1.287317 -2.337283
b -1.677615 -0.469706 -4.347631
c -0.699353 -2.280233 -2.571634
C a 1.224786 -0.888989 0.653669
b -1.183633 -0.071378 -1.356680
c -0.205371 -1.881905 0.419317
IIUIC, Here's one way, using merge on temporary k, resulting in every index combination and then groupby on columns.
In [192]: (df1.reset_index().assign(k='k').merge(df2.assign(k='k').reset_index(), on=['k'])
.set_index(['index_x', 'index_y'])
.groupby(lambda x:x.split('_')[0], axis=1)
.sum()
.drop('k', 1))
Out[192]:
X Y Z
index_x index_y
A a -2.281005 -1.606760 -0.853813
b -2.683788 -2.487876 2.471459
c -0.333471 -2.155734 1.688883
B a -0.790146 0.074629 -2.368680
b -1.192928 -0.806487 0.956592
c 1.157388 -0.474345 0.174017
C a -2.114412 0.100412 -2.352661
b -2.517195 -0.780704 0.972611
c -0.166878 -0.448562 0.190036
Details
In [193]: df1
Out[193]:
X Y Z
A -1.087129 -1.264522 1.147618
B 0.403731 0.416867 -0.367249
C -0.920536 0.442650 -0.351229
In [194]: df2
Out[194]:
X Y Z
a -1.193876 -0.342237 -2.001431
b -1.596659 -1.223354 1.323841
c 0.753658 -0.891211 0.541265
In [196]: (df1.reset_index().assign(k='k').merge(df2.assign(k='k').reset_index(), on=['k'])
.set_index(['index_x', 'index_y']))
Out[196]:
X_x Y_x Z_x k X_y Y_y Z_y
index_x index_y
A a -1.087129 -1.264522 1.147618 k -1.193876 -0.342237 -2.001431
b -1.087129 -1.264522 1.147618 k -1.596659 -1.223354 1.323841
c -1.087129 -1.264522 1.147618 k 0.753658 -0.891211 0.541265
B a 0.403731 0.416867 -0.367249 k -1.193876 -0.342237 -2.001431
b 0.403731 0.416867 -0.367249 k -1.596659 -1.223354 1.323841
c 0.403731 0.416867 -0.367249 k 0.753658 -0.891211 0.541265
C a -0.920536 0.442650 -0.351229 k -1.193876 -0.342237 -2.001431
b -0.920536 0.442650 -0.351229 k -1.596659 -1.223354 1.323841
c -0.920536 0.442650 -0.351229 k 0.753658 -0.891211 0.541265

Priority 8-to-3 encoder in Verilog (case, casex)

I'm trying to describe a SN54LS348 element (8-line to 3-line priority encoder).
The truth table is:
INPUTS OUTPUTS
E | 0 1 2 3 4 5 6 7 ** A2 A1 A0 | GS EO
///////////////////////////////////////
H | X X X X X X X X ** Z Z Z | H H
L | H H H H H H H H ** Z Z Z | H L
L | X X X X X X X L ** L L L | L H
L | X X X X X X L H ** L L H | L H
L | X X X X X L H H ** L H L | L H
L | X X X X L H H H ** L H H | L H
L | X X X L H H H H ** H L L | L H
L | X X L H H H H H ** H L H | L H
L | X L H H H H H H ** H H L | L H
L | L H H H H H H H ** H H H | L H
Here's my implementation:
module L348 (E, D0, D1, D2, D3, D4, D5, D6, D7, A0, A1, A2, GS, EO);
input E, D0, D1, D2, D3, D4, D5, D6, D7;
output A0, A1, A2, GS, EO;
assign D = {D0, D1, D2, D3, D4, D5, D6, D7};
parameter HIGH_IMPEDANCE = 3'bz;
reg [7:0] MASK_1 = 8'b0000_0001;
reg [7:0] MASK_2 = 8'b0000_0011;
reg [7:0] MASK_3 = 8'b0000_0111;
reg [7:0] MASK_4 = 8'b0000_1111;
reg [7:0] MASK_5 = 8'b0001_1111;
reg [7:0] MASK_6 = 8'b0011_1111;
reg [7:0] MASK_7 = 8'b0111_1111;
reg [7:0] MASK_8 = 8'b1111_1111;
reg [2:0] A;
reg [1:0] GS_EO;
reg [7:0] temp;
reg [7:0] mem [7:0];
initial
begin
mem[0] = MASK_1;
mem[1] = MASK_2;
mem[2] = MASK_3;
mem[3] = MASK_4;
mem[4] = MASK_5;
mem[5] = MASK_6;
mem[6] = MASK_7;
mem[7] = MASK_8;
temp = 8'bxxxx_xxxx;
end
assign {A2, A1, A0} = A;
assign {GS, EO} = GS_EO;
integer i;
always #(*)
begin
for (i = 7; i > 0; i = i - 1)
if (mem[i] & D == mem[i])
begin
temp = mem[i];
i = -1;
end
if (E)
begin
A = HIGH_IMPEDANCE;
GS_EO = 2'b11;
end
else
begin
if (temp == 8'b1111_1111)
begin
A = HIGH_IMPEDANCE;
GS_EO = 2'b10;
end
else
begin
GS_EO = 2'b01;
case (temp)
8'b0000_0001: A = 3'b001;
8'b0000_0011: A = 3'b010;
8'b0000_0111: A = 3'b011;
8'b0000_1111: A = 3'b100;
8'b0001_1111: A = 3'b101;
8'b0011_1111: A = 3'b110;
8'b0111_1111: A = 3'b111;
endcase
end
end
end
endmodule
It fails to achieve the switching of signals A2-A0 which are always in a X-state (except when E = H). I've tried many solutions, but it feels like simulator can't manage 'case' block ( I tried also 'casex' block). There is a bug somewhere, but I can't figure it out. Does anyone have ideas?
You've got quite a few things going on here but your most immediate problem is probably.
assign D = {D0, D1, D2, D3, D4, D5, D6, D7};
This implicitly defined wire is only going to be 1 bit wide and so the high 7b are going to be dropped. Isn't Verilog fun?
There are other logical problems but the easiest way of doing a priority encoder with a case statement is as follows:
casez (in)
4'b???1 : out = 0;
4'b??10 : out = 1;
4'b?100 : out = 2;
4'b1000 : out = 3;
default : out = 0; //no match
endcase
The casez allow you to put in ? for don't care conditions similar to your truth table. The first matching entry is taken which give you the priority behavior.
Adapt as needed for your case for width, direction of priority, width of IO, etc...
Finally as a stylistic concern your early loop termination should use break rather than directly modifying the loop variable.

Edit text in file(UTF16)

I want replace 1 word in text file (file format is not .txt)
file Unicode is (UTF16)
few text example:
I D = " f f 0 3 4 a 9 2 - d d 9 f - 4 3 7 4 - a 8 a d - f 5 5 4 0 0 2 a 4 1 9 b " I S S U E _ D A T E = " 2 0 1 7 - 0 2 - 1 6 T 1 7 : 2 9 : 1 8 . 9 7 0 2 2 9 4 Z " S E Q U E N C E = " 0 " M A N A G I N G _ A P P L I C A T I O N _ T O K E N = " " > < L I C E N S E P U B L I C _ I D = " 3 A A - U J F - 8 K P " U S E R N A M E = " N d a G 6 Z T w u v I X Z B i t h 8 g o d d Q x E r x 0 + O g M c t 0 2 3 f X K O E w = " P A S S W O R D = " F 9 b n 6 b v w l f I 5 Z A 2 t h M h 9 d d s x Q L w = " T Y P E = " T R I A L " F L A G S = " 4 " D I S P L A Y _ N A M E =
I want change T R I A L to other word
It's not too hard to modify your text file. Use the IO class to assign it to a text file, then use String.Replace(oldValue As String, newValue As String) to change your string. Then use IO again to save the string to the file. This should work so long as your file isn't open and being used in another program - regardless of file extensions.
An example, to help you, could be something such as this:
Dim myFileContents as String = IO.File.ReadAllText("Path\To\My\File\File.extension")
myFileContents = myFileContents.Replace("T R I A L", "Some other word")
IO.File.WriteAllText("Path\To\My\File\File.extension", myFileContents)
Modify the contents to suit your situation - however, this is only a basic implementation. Additionally, it is important to note that String.Replace() will change all occurrences of your word to the new word.