LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-16-2013, 01:10 PM   #31
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,502

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415

Quote:
Originally Posted by tabbygirl1990 View Post
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

Last edited by danielbmartin; 10-16-2013 at 01:16 PM. Reason: Correct t7po
 
1 members found this post helpful.
Old 10-16-2013, 01:52 PM   #32
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
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?
 
Old 10-16-2013, 02:18 PM   #33
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,502

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by tabbygirl1990 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Bash] Totalling & Averaging in one go blenderfox Programming 19 07-07-2013 06:32 AM
Averaging columns from multiple files carlr Programming 3 03-18-2012 02:24 AM
alsa - averaging stereo to mono for speaker output bdjnk Linux - General 0 08-18-2009 03:38 PM


All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration