export variable from awk

I have this rather simple script

#!/bin/bash

awk -v channels=4096 '{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{printf "There are %d detectors\n ",detectors=NF;}1' $1.dat >$1_Processed.dat
rm -f $1.dat
printf "There are %d detectors\n",$detectors
exit 0

What I am trying to do is print the number of fields(NF) that awk reads. As a result I always get 0 for the variable detectors.

Is there a way around this?

EDIT

The full script is

#!/bin/bash

if test $1 ; then
   if [ -f $1.evnt ] ; then
      rm -f $1.dat
      sed -n '2p' $1.evnt | (read v1 v2 v3
      for filename in $1*.evnt ; do
         echo -e "Processing file $filename"
         sed '$d' < $filename > $1_tmp
         sed -i '/Kmax/d' $1_tmp
         sed -i '/^'"$v1"' '"$v2"' /d' $1_tmp
         cat $1_tmp >> $1.dat
      done
      v3=`wc -l $1.dat | awk '{print $1}' `
      echo -e "$v1 $v2 $v3" > .$1.dat
      rm -f $1_tmp)
   else
      echo -e "\a!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
      echo -e "  Event file $1.evnt doesn't exist  !!!!!!"
      echo -e "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
   fi   
else
   echo -e "\a!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
   echo -e "!!!!!  Give name for event files  !!!!!"
   echo -e "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
fi
detectors=$(awk -v channels=4096 '{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{print NF;}1' $1.dat >$1_Processed.dat)
#awk -v channels=4096 '{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{printf "There are %d detectors\n ",detectors=NF;}1' $1.dat >$1_Processed.dat
rm -f $1.dat
printf "There are %d detectors\n",$detectors
exit 0

A typical input file is

Kmax Event File - Text Format
1 6 1000 
1623 5730 8209 12307 16398 20495 
1505 5611 8208 12306 16395 20497 
934 5036 8208 12308 16399 20498 
1552 5656 8208 12306 16396 20495 
1622 5729 8208 12305 16397 20495 
1526 5631 8207 12305 16397 20496 
1522 5629 8208 12307 16396 20497 
1525 5630 8208 12306 16399 20496 
1625 5730 8208 12305 16396 20496 
1717 5822 8207 12305 16398 20495 
1690 5796 8209 12306 16397 20496 
1525 5630 8209 12305 16399 20495 
1625 5731 8208 12305 16397 20495 
1523 5629 8209 12307 16398 20497 
1719 5825 8208 12307 16398 20495 
1714 5821 8207 12305 16395 20496 
1525 5630 8208 12306 16399 20495 
1528 5634 8209 12306 16396 20496 
1624 5729 8208 12307 16397 20496 
1625 5729 8208 12305 16397 20495 
1524 5630 8209 12305 16397 20495 
1393 5497 8207 12306 16397 20495

Answers


You probably want this:

detectors=$(
    awk -v channels=4096 '
        {for (i=1;i<=NF;i++) $i=$i-(i-1)*channels; print} 
        END {print NF > "/dev/stderr"}
    ' $1.dat 2>&1 >$1_Processed.dat
)

The redirections:

  • first, fd 2 is redirected to the whatever fd 1 is currently pointing to
  • then fd 1 is redirected to a file (this does not affect what fd 2 is now pointing to)
  • testing:

    $ x=$(awk 'BEGIN {print "stdout"; print "stderr" > "/dev/stderr"}' 2>&1 >/tmp/foo)
    $ echo "$x"
    stderr
    $ cat /tmp/foo
    stdout
    

The form print NF > "/dev/stderr" may be specific to GNU awk.

I notice that the 2nd field of the 2nd line is also 6. If that is the number of detectors, it is a more obvious way to extract it.

detectors=$( awk 'NR==2 {print $2; exit}' $1.dat )
# or
read a detectors b < <(sed -n '2{p;q}' $1.dat)

Need Your Help

Finding gaps in concurrent date ranges - MySQL

mysql sql date date-range

I have a table like the one below. In reality there are 50,000 users, and a technically infinite number of ranges for each user. There is no limit on date gaps, starts, ends, overlaps, etc.

How to add new product packs to cart?

php prestashop prestashop-1.6

I want to add pack product to cart programatically, my action in ModuleFrontController is viewed like this: