LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   two line averaging (https://www.linuxquestions.org/questions/linux-newbie-8/two-line-averaging-4175480103/)

danielbmartin 10-16-2013 12:10 PM

Quote:

Originally Posted by tabbygirl1990 (Post 5046960)
i must be somehow running your script wrong?

My program is not an awk program, it is a bash program which uses the awk command.

On my computer the script file is named /home/daniel/Desktop/LQfiles/dbm942.bin
and the input file is named /home/daniel/Desktop/LQfiles/dbm942inp.txt
You would choose different names.

I run the program by launching a terminal session and entering: bash /home/daniel/Desktop/LQfiles/dbm942.bin

Here is my script, in it's entirety. Note that it contains two versions; you would use either one.

Daniel B. Martin

Code:

#!/bin/bash
#  Daniel B. Martin  Oct13
#
#  To execute this program, launch a terminal session and enter:
#  bash /home/daniel/Desktop/LQfiles/dbm942.bin
#
#  This program inspired by:
#    http://www.linuxquestions.org/questions/linux-newbie-8/
#      two-line-averaging-4175480103/#post5046214

# Interpolation -- interspersing adjacent lines with numerical averages

# File identification
  Path=$(cut -d'.' -f1 <<< ${0})
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"


echo; echo "Method #2 of LQ Member danielbmartin"
awk 'BEGIN{getline;
  for (i=2;i<=NF;i++) {printf "%8.2f",$i;          a[i]=$i} print ""};
  {for (j=2;j<=NF;j++) {printf "%8.2f",(a[j]+$j)/2; a[j]=$j} print "";
  {for (k=2;k<=NF;k++)  printf "%8.2f",a[k];                print ""}}' $InFile  \
|nl |sed 's/.00//' >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File"

echo; echo "Method #3 of LQ Member danielbmartin"
awk 'BEGIN{getline;
  for (i=2;i<=NF;i++)  printf "%8.2f",      $i;    a[i]=$i; print ""};
  {for (j=2;j<=NF;j++) {printf "%8.2f",(a[j]+$j)/2; a[j]=$j} print "";
  {for (k=2;k<=NF;k++)  printf "%8.2f", a[k];                print ""}}' $InFile  \
|awk '{$1=NR" "int($1); print}' >$OutFile
echo "OutFile ..."; cat $OutFile; echo "End Of File"


echo; echo "Normal end of job."; echo; exit


tabbygirl1990 10-16-2013 12:52 PM

well that certainly makes it run

but i have used some awk scripts that start out with an awk command, usually awk -F, and then i chmod them to be executeable and then run them by

Code:

awk_script_name input.dat > output.dat
so how do i know when it's a bash script with an awk command or it's an awk script? is it by the #! tag being either #!/bin/bash or #!/bin/awk but your bash script didn't have that?

danielbmartin 10-16-2013 01:18 PM

Quote:

Originally Posted by tabbygirl1990 (Post 5046986)
well that certainly makes it run

but i have used some awk scripts that start out with an awk command, usually awk -F, and then i chmod them to be executeable and then run them by

Code:

awk_script_name input.dat > output.dat
so how do i know when it's a bash script with an awk command or it's an awk script? is it by the #! tag being either #!/bin/bash or #!/bin/awk but your bash script didn't have that?

My program dbm942.bin starts with #!/bin/bash. That makes it a bash program.

I know that it is possible to write a pure awk program and execute it as such, but I've never done that and don't know what advantages there are (if any) to doing so.

I do programming as a recreation and am entirely self-taught in Linux. I use a bash shell and have no reason for this other than I've followed the example of other programs posted on this site. Using bash lets me use awk and pipe it with other Linux commands such as grep, sed, cut, paste, etc.

I wrote the version "Method #3" for you, in case you know awk but are not familiar with nl and sed.

Daniel B. Martin


All times are GMT -5. The time now is 08:34 AM.