0

On the bash shell I use the command

cat filename | cut -d, -f4 | sort -n | awk '
  BEGIN {
    c = 0;
    sum = 0;
  }
  $1 ~ /^[0-9]*(\.[0-9]*)?$/ {
    a[c++] = $1;
    sum += $1;
  }
  END {
    ave = sum / c;
    if( (c % 2) == 1 ) {
      median = a[ int(c/2) ];
    } else {
      median = ( a[c/2] + a[c/2-1] ) / 2;
    }
    OFS="\t";
    print sum, c, ave, median, a[0], a[c-1];
  }
'

This works fine when run from terminal But my shell script shows awk usage when trying to use the above commands inside a script and run the script. I did

#!/bin/sh

diffFile="del.csv"



x=$(cat $diffFile | cut -d, -f4 | sort -n | awk 
  'BEGIN {
    c = 0;
    sum = 0;
  }
  $1 ~ /^[0-9]*(\.[0-9]*)?$/ {
    a[c++] = $1;
    sum += $1;
  }
  END {
    ave = sum / c;
    if( (c % 2) == 1 ) {
      median = a[ int(c/2) ];
    } else {
      median = ( a[c/2] + a[c/2-1] ) / 2;
    }
    OFS="\t";
    print sum, c, ave, median, a[0], a[c-1];
  }'
)

echo "$x"

But this shows awk usage on running the script. How do I fix this.

pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
  • 1
    You have a small difference in capitalization when using your filename variable. `diffFile` vs. `$diffFIle`. I assume that's causing `cat` to wait for standard input, causing the freeze. – jas Mar 07 '17 at 13:14
  • @jas thanks for pointing out. Now on running the script it shows awk usage :( – pythonRcpp Mar 07 '17 at 13:16
  • Well, that's some kind of progress at least :-). Try starting your awk script on the same line as the command, or alternatively escape the newline with a backslash. – jas Mar 07 '17 at 13:18
  • 3
    You need to escape the newline before the awk command, i.e. write `x=$(cat $diffFile | cut -d, -f4 | sort -n | awk \ `. Otherwise, bash will think you try to invoke awk with no arguments. – Aserre Mar 07 '17 at 13:18
  • put your variable in back tick like below - " x=\`cat dict.txt | cut -d'|' -f4 | sort -n|awk '{print $0}'\`" – VIPIN KUMAR Mar 07 '17 at 13:20
  • 2
    @VIPINKUMAR in general, backticks are strongly discouraged for `bash` due to easy syntax errors. the `$(...)` syntax does exactly the same and is more readable – Aserre Mar 07 '17 at 13:22
  • 3
    Note the difference of where you put the initial single quote in the command line version that works. Do the same in the script version and you should be fine. – jas Mar 07 '17 at 13:24
  • Can you please put the single quote with awk like below - x=$(cat $diffFile | cut -d, -f4 | sort -n | awk ' – VIPIN KUMAR Mar 07 '17 at 13:35
  • @pythonRcpp - Add "\" at the end of awk in x variable line or try above solution. – VIPIN KUMAR Mar 07 '17 at 13:45
  • Thanks guys, it works ! What should I do now. @aserre jas. should I delete the question now ? – pythonRcpp Mar 07 '17 at 13:48
  • There are several questions that could be duplicates for your question. [this one is the most upvoted](http://stackoverflow.com/questions/18599711/how-can-i-split-a-shell-command-over-multiple-lines-when-using-an-if-statement), and [this one](http://unix.stackexchange.com/questions/82182/having-multi-lines-in-bash-command-substitution) is almost exactly similar but is on a different SE site – Aserre Mar 07 '17 at 13:55

0 Answers0