1

Hi im having troubles with the following code:

#|/bin/tcsh
TOTALMOS=`grep -v "Last MO:" x.log | grep "Total: .* MOs" | grep "Total: .* MOs" | awk '{ print $2 }' | tr '\n' ' ' | awk '{ print $1 }'`
RADIONO=`grep -n "get radio no" x.log | cut -f1 -d:`
RADIONO2=`expr "$RADIONO" + 6`
RADIONO3=`expr "$RADIONO2" + "$TOTALMOS"`
SED=`sed -n ''$RADIONO2','$RADIONO3'p' x.log | awk '{ print $3 }' | tr '\n' '  '`
echo "$SED"

The output is the following:

5 8 2 4 0 10 6 11 1 3 1 9  

Pease note: there are two breaks at the end of the script being "...1 9__"

Well what i really want it is SUM all the numbers excluding the final breaks that idk where they come from. My first thought was substitute the newlines with "+" and then make it work with expr but it does not work at all...

Please any advice?

Thanks in advance!

Best Regards.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Never write [t]csh scripts - Google "csh why not". Edit your question to show a representative test case with sample input and output so we can help you write a concise awk script to do everything you are trying to do with that mush of shell, awk and a dozen other tools and pipelines. – Ed Morton Aug 24 '15 at 21:43
  • 1
    Do not use the `sh` tag for shells incompatible with the POSIX sh standard! – Charles Duffy Aug 24 '15 at 21:49
  • Why not work with csh? They say its very like C and i wanted to start with some lenguage that will work for me in the future, im just a beginner. But i want to learn all related with linux to improve my skills at work! – Αλέξανδρος Γιος του Ήρας Aug 25 '15 at 22:28
  • Do you really want to know?: [Top ten reason to not use csh](http://www.grymoire.com/unix/CshTop10.txt) –  Aug 26 '15 at 00:28

1 Answers1

1

awk to the rescue

$ echo "1 2 3 4 5 6 7    " | awk -vRS=" " '{sum+=$0} END{print sum}'
28
$ echo "1 2 3 4 5 6 7    " | awk '{for(i=1;i<=NF;i++) sum+=i; print sum}'                    
28

these two will scripts both sum up the numbers ignoring the trailing blanks if it's what you mean by "two breaks at the end of the script".

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Thanks!! I'll try it tomorrow at work, idk why some commands don't work on Ubuntu 14 and work at my workplace, they use unix buy an old build i think. I meant just what you said i wanted to ignore the trailing blanks, (i don't know why they show up...) but my english is a little bit but sorry. Thanks again – Αλέξανδρος Γιος του Ήρας Aug 25 '15 at 22:22