LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Your help (https://www.linuxquestions.org/questions/programming-9/your-help-609468/)

Gins 12-27-2007 03:10 PM

Your help
 
I am running 'open SuSE Linux 10.0 version.
Everything works fine with my Linux.

Please look at the following:

#!/bin/awk -f
BEGIN {
# How many lines
lines=0;
total=0;
}
{
# this code is executed once for each line
# increase the number of files
lines++;
# increase the total size, which is field #1
total+=$1;
}
END {
# end, now output the total
print lines " lines read";
print "total is ", total;
if (lines > 0 ) {
print "average is ", total/lines;
} else {
print "average is 0";
}
}
---------------------------------------------------------------------------
I wrote the above program using the vi editor.
The name of the progrmme is 'average_awk.c
Afterwards, I made it executable by doing the following:
chmod 755 average_awk.c.

In order to run the program, I wrote the following and pressed enter.

./average_awk.c


I got the following output. What is the problem?
Ni@linux-3vxw:~/C++_and_C> ./average_awk.c
./average_awk.c: line 5: BEGIN: command not found
./average_awk.c: line 9: syntax error near unexpected token `}'
./average_awk.c: line 9: `}'
Ni@linux-3vxw:~/C++_and_C>

[ I have a folder named 'C++_and_C' for writing C, C++ and Shell Scripts.]

acid_kewpie 12-27-2007 03:20 PM

you have 1,000+ posts, you have no excuse to use poor thread titles... what does "your help" say about your problem?? and why are you writing awk code in a .c file?? it's not c code... the file appears to being run as a shell script... what if you run it explicitly through awk? "awk -f average_awk.c"

Gins 12-27-2007 03:25 PM

Thanks acid_kewpie for the comments.
I apologize for writing an unsuitable title.

I am not good at awk at all.
How do I run it as an awk program?

acid_kewpie 12-27-2007 03:33 PM

I've already given you the exact command.... merry christmas.

Gins 12-27-2007 03:48 PM

It did not work.

Ni@linux-3vxw:~/C++_and_C> awk-f average_awk.c
bash: awk-f: command not found
Ni@linux-3vxw:~/C++_and_C>

Uncle_Theodore 12-27-2007 03:59 PM

You need a space between "awk" and "-f"

PAix 12-27-2007 04:03 PM

Code:

awk-f average_awk.c
Not your day I'm afraid Gins.
Code:

awk <space> -f <space>average_awk.c
If you go down to the woods today, try not to step into the u-know-what.
The -f specifies that what follows is a filename containing code and not code directly.

Gins 12-27-2007 04:06 PM

It worked and drew a blank.
Is the following fine?


Ni@linux-3vxw:~/C++_and_C> awk -faverage_awk.c

Uncle_Theodore 12-27-2007 04:08 PM

OK, you got me. You need yet another space between "-f" and the name of your file, like PAix said. :)
You may also look at some man awk.

Gins 12-27-2007 04:09 PM

Thanks
It worked now.

Ni@linux-3vxw:~/C++_and_C> awk -f average_awk.c
0 lines read
total is 0
average is 0
Ni@linux-3vxw:~/C++_and_C>

Gins 12-27-2007 04:13 PM

I don't know much about awk and sed.

They are editors or rather programs to find words, phrases in text files.

How on earth they work as programming languages?
Do you consider sed and awk as programming langusges?

Shell Scripting, C, and C++ are programming languages.

Uncle_Theodore 12-27-2007 04:38 PM

Sed is a stream editor, but awk is actually a programmin language. Just like C or bash. Like I said, read man awk. :)

PAix 12-27-2007 04:48 PM

Hi Gins,
SED will handle files that are too big for some regular in memory editors so is truly a great workhorse, but I am not overly familiar with it. Awk is a pattern matching language and I believe was written by the Holy Trinity to meet a need for some of the functionality of C without the footprint. I became interested in it a few years ago when I wanted access to the C like functions but C was outside my remit. Everyone at the time wrote Bourne shell scripts that included the inevitable one liner awk scripts to do what at the time were decided to be difficult corner cases - I was using Solaris at the time. I initially found awk/nawk to be somewhat different to most of the other languages that I had encountered, but really came to love it. It just requires a slight adjustment of the mindset to understand it and bingo once the penny drops it's good. I have learned a few new tricks from Ghostdog74's contributions to this forum. The language is described in a book by Aho, Weinberger and Kernighan - so it's provenance re C is well established. The book isn't very thick at all and is an ideal desktop reference if the internet isn't at hand.
Pdf manual.

ghostdog74 12-27-2007 10:07 PM

Quote:

Originally Posted by Gins (Post 3003286)
I don't know much about awk and sed.

then learn it if you are interested.

Gins 12-28-2007 04:34 AM

I thank Uncle, PAix and Ghost for the excellent replies.

The problem is there are so many programming languages and other stuff in the domain of computer science.

Who on earth can learn all those languages and other stuff?

Now I have learnt there are other flavours of awk. They are gawk and nawk.

The pdf file which PAix suggested is not awk. It is nawk.

I have a smattering of C++ , C and Shell Scripting.

I believe you are all working with Sed and Awk from time to time. If it is the case, you are somewhat familiar with all the nuances.


You wrote how to run a awk a program. You wrote the following.

awk -f 'The name of the programme'

As you are working with awk and sed, you know those things.


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