0

The output of my script:

                Ch0    Ch1    Ch2    Ch3
Sample 0:       0x1a03 0x1a03 0x4a03 0x5703
Sample 1:       0x4b03 0x4403 0x1e03 0x0904
Sample 2:       0x1003 0x1903 0x4003 0xae03
Sample 3:       0x1e03 0x2603 0x3303 0xad03
Sample 4:       0x1003 0x8403 0x4303 0x6203
Sample 5:       0xe003 0x1603 0x3403 0xc403
Sample 6:       0xf802 0x3b03 0x5303 0x6103

with no arguments prints a row with column headers, and since what would normally be row two according to sed or awk, is a line that starts with "Sample 0:", I have devised a way to get around that if someone is passing an argument of one or zero:

if [ $sample != 0 ]; then 
  let sample=$((sample+2)) 
else 
  let sample=$((sample+1)) 
fi 

If I run the script like this, I get nonconsecutive values:

./sample4.sh | sed -n '1'p; ./sample4.sh | sed -n '3p;5p;7p'
                Ch0    Ch1    Ch2    Ch3
Sample 1:       0x4b03 0x4403 0x1e03 0x0904
Sample 3:       0x1e03 0x2603 0x3303 0xad03
Sample 5:       0xe003 0x1603 0x3403 0xc403

For total sample count I can get it like this:

let total=$(./sample4.sh | wc -l);echo "Total Sample Count: $((total-1))"

I get a range of rows like this:

./sample4.sh | sed -n '1'p; ./sample4.sh | sed -n '2,11p'
                Ch0    Ch1    Ch2    Ch3
Sample 0:       0x1a03 0x1a03 0x4a03 0x5703
Sample 1:       0x4b03 0x4403 0x1e03 0x0904
Sample 2:       0x1003 0x1903 0x4003 0xae03
Sample 3:       0x1e03 0x2603 0x3303 0xad03
Sample 4:       0x1003 0x8403 0x4303 0x6203
Sample 5:       0xe003 0x1603 0x3403 0xc403
Sample 6:       0xf802 0x3b03 0x5303 0x6103
Sample 7:       0x1003 0x1503 0x4203 0x5803
Sample 8:       0x2303 0x1f03 0x5703 0x6203
Sample 9:       0x1703 0x7303 0x3103 0x3303

I'm trying to figure out how I interpret hyphens or commas, i.e..., range-values and non,consecutive,values, i.e..., ./sample4.sh -s 0-10 -c 0,3 passed into the following script:

#!/usr/bin/env bash

while getopts ':c:s:t' opt; do
    case $opt in
        s) samps="$OPTARG" ;;
        c) chans="$OPTARG" ;;
        t) totReq=1 ;;
        *) printf 'Unrecognized option "%s"\n' "$opt" >&2
    esac
done
shift $(( OPTIND - 1 ))

printf 'samps="%s"\n' "$samps" >&2
printf 'chans="%s"\n' "$chans" >&2
printf 'totreq="%d"\n' "$totReq" >&2

hexdump -v -e '8/1 "%02x " "\n"' samples.bin |
awk -v samps="$samps" -v chans="$chans" -v totReq="$totReq" '
    BEGIN {
        printf "\t\tCh0    Ch1    Ch2    Ch3\n"        # print the header line
    }
    {
        printf("Sample %d:", NR - 1)                            # print the sample number
        printf(substr("        ", 1, 8 - length(NR - 1)))       # adjust the length of the spaces
        for (i = 1; i <= NF; i+=2) {                            # print every two nibbles
            j = i + 1
            printf("0x%s%s%s", $i, $j, j == NF ? ORS : OFS)
        }
    }
'

1 Answers1

0

So split the string on comma, and for each element, if the element has a hyphen, split it on hyphen, and for each number in range, add the number to some array, and if the element has no hyphen, add that element as a number to some array. Then filter lines depending on elements in that array. Something along:

awk -s argsamples="$samples" '
   BEGIN {
      split(argsamples, tmp, ",")
      for (i in tmp) {
          if (tmp[i] ~ "-") {
             split(tmp, tmp2, "-")
             for (i = tmp2[0]; i < tmp2[1]; ++i) {
                  samples[i]=1
             }
          } else {
             samples[tmp[i]]=1
          }
      }
   }
   NR in samples{ print }
'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111