Differences with gawk on cmd vs cygwin

I have this script (thanks Don!) that runs as expected in cygwin :

gawk -F, 'NR == 1{print "Source,Well_Source,Volume,Destination_Well,Destination"; OFS = ","; next}/Ladder/{next}LAST2 && $2 != LAST2{print "PCR_Plate", ++CNT, (SUM > 10)?800/SUM:0, 1, "Deadpool"; SUM = 0}{SUM += ( $4 > 430 && $4 < 490 && $5 > 45 ) ? $5 : 0; LAST2  = $2}' Process1.txt

However, when I run it on cmd :

C:\cygwin64\bin\gawk -F, 'NR == 1{print "Source,Well_Source,Volume,Destination_Well,Destination"; OFS = ","; next}/Ladder/{next}LAST2 && $2 != LAST2{print "PCR_Plate", ++CNT, (SUM > 10)?800/SUM:0, 1, "Deadpool"; SUM = 0}{SUM += ( $4 > 430 && $4 < 490 && $5 > 45 ) ? $5 : 0; LAST2  = $2}' Process1.txt

It just goes idle. The first part prints fine:

C:\cygwin64\bin\gawk -F, 'NR == 1{print "Source,Well_Source,Volume,Destination_Well,Destination"; OFS = ","; next}' Process1.txt

I have tried a bunch of different things to make it work with no luck. Any help will be greatly appreciated!
PS. I am attaching the infile ( Process1.txt )

This is probably a quoting issue, which works differently in Windows. Try putting the script itself in a script file :

BEGIN { 
  FS="," 
} 

NR == 1 {
  print "Source,Well_Source,Volume,Destination_Well,Destination"
  OFS = ","
  next
}

/Ladder/ {
  next
}

LAST2 && $2 != LAST2 {
  print "PCR_Plate", ++CNT, (SUM > 10)?800/SUM:0, 1, "Deadpool"; SUM = 0
}

{
  SUM += ( $4 > 430 && $4 < 490 && $5 > 45 ) ? $5 : 0
  LAST2  = $2
}

and call the command like this:

awk -f this_script Process1.txt
1 Like

That worked like a charm! Thanks a TON!
PS. Still, I am bit curious, would it be possible to fix the script in such way that can be run directly from the cmd terminal?

I have no way to test it, since I have no Windows, but could you give this a try:

C:\cygwin64\bin\gawk -F, "NR == 1{print \"Source,Well_Source,Volume,Destination_Well,Destination\"; OFS = \",\"; next}/Ladder/{next}LAST2 && $2 != LAST2{print \"PCR_Plate\", ++CNT, (SUM > 10)?800/SUM:0, 1, \"Deadpool\"; SUM = 0}{SUM += ( $4 > 430 && $4 < 490 && $5 > 45 ) ? $5 : 0; LAST2  = $2}" Process1.txt

--
Otherwise try:

C:\cygwin64\bin\gawk -F, "NR == 1{print \"Source,Well_Source,Volume,Destination_Well,Destination\"; OFS = \",\"; next}/Ladder/{next}LAST2 && \$2 != LAST2{print \"PCR_Plate\", ++CNT, (SUM > 10)?800/SUM:0, 1, \"Deadpool\"; SUM = 0}{SUM += ( \$4 > 430 && \$4 < 490 && \$5 > 45 ) ? \$5 : 0; LAST2  = \$2}" Process1.txt
1 Like

That is tricky. It probably is, but may be gawk.exe-dependent as much as cmd dependent. Quoting in CPM/DOS/Windows has traditionally been a free-for-all, with much implementation left up to whatever command you're using.

Scrutinizer
Your first did the trick
Thanks!