Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 12:16 PM.
Reason: Correct t7po
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63
Original Poster
Rep:
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.