4 ms·
For absolute beginners I always recommend: If you don't have a Mac and are not on BSD or Linux: * set up a Linux machine (this can be a computer running Lin
by zmix 7y ago
For absolute beginners I always recommend:
If you don't have a Mac and are not on BSD or Linux:
* set up a Linux machine (this can be a computer running Linux or a virtual machine)
* install (usually it comes pre-installed) GNU AWK aka: 'gawk'.
* head over to https://www.gnu.org/software/gawk/manual/html_node/index.html and start with https://www.gnu.org/software/gawk/manual/html_node/Getting-Started.html#Getting-Started
* also make sure to read https://www.gnu.org/software/gawk/manual/html_node/Basic-Concepts.html#Basic-Concepts either early on, or when you switch from 'declarative' to 'imperative' (see below)
Why 'awk'?
It is one of the standard, classical UNIX utilities. Learning how to use it will teach you how to use all command line utilities, including 'man awk'. Since you use the GNU implementation, you will also learn both short and long options/arguments.
You will also gain basic knowledge of the terminal (navigating paths, etc.)
You will also learn the concept of StdIn, StdOut and StdErr.
You will learn what a 'stream of data' is, on a basic level.
It has been co-authored by Brian Kernighan. He also co-authored C. The 'awk' language is a C-family language, which means, that, once understood, you will be able to read the control flow of C, C++, ObjectiveC, C#, PHP, Go, Swift, Javascript, Java, R, D and many others: https://en.wikipedia.org/wiki/List_of_C-family_programming_languages https://en.wikipedia.org/wiki/List_of_C-family_programming_l.... The familiarity to Javascript is exceptionally striking: by just changing a few little things one can run some basic 'awk' scripts on a Javascript interpreter. Here is an example from [Rosetta Code (100 Doors)](http://rosettacode.org/wiki/100_doors#AWK http://rosettacode.org/wiki/100_doors#AWK):
BEGIN {
for(i=1; i <= 100; i++)
{
doors[i] = 0 # close the doors
}
for(i=1; i <= 100; i++)
{
for(j=i; j <= 100; j += i)
{
doors[j] = (doors[j]+1) % 2
}
}
for(i=1; i <= 100; i++)
{
print i, doors[i] ? "open" : "close"
}
}
Now remove the 'BEGIN {}' part, replace the '#' with '//' (or remove the
comment altogether), change 'print i' into 'console.log(i)' and put a 'var
doors=[]' at the beginning of the script. It should look now like this:
var doors=[]
for(i=1; i <= 100; i++)
{
doors[i] = 0 // close the doors
}
for(i=1; i <= 100; i++)
{
for(j=i; j <= 100; j += i)
{
doors[j] = (doors[j]+1) % 2
}
}
for(i=1; i <= 100; i++)
{
console.log(i, doors[i] ? "open" : "close")
}
save it as '100doors.js' and execute it with NodeJS.
'AWK' is more than just a programming language. It is a processor, that has a programming language 'built-in'. For a novice this means, that the learning curve is very shallow: before you start programming, you will learn some more basics:
* what patterns and pattern rules are
* how to use pattern substitution
All this happens on the plainest data format, which exists: human readable text! As you have understood these two concepts, and how 'awk' handles them, you have gained knowledge, that you immediately can use in your everyday's computing extravaganza. Much better than learning examples for 'factorial', 'fibonacci', etc.
You should have understood these things in your first few days, if you are totally new to working with a computer on this level, if you already have some primitive knowledge, it is just a few hours.
Congratulations, you now are able to do 'declarative' template programming! Declarative means, that you tell the computer, what you want, not how to do it. It also means, that you do not need to care for file-handling, parsing and string tokenization, etc. yourself.
Once you got this, stuff becomes more serious. You now will learn 'imperative' programming, which means to tell the computer how to do it!
'AWK' is a very good first language, because it has dynamic typing (which means, that you do not need to care for variable declarations and, especially, typing, which can become a whole topic on its own, so better to leave that for a time, when you are more secure) and only knows about three different types:
* string
* number
* array
The function catalog is also very basic. And these functions will reappear, the one way or the other, in all other languages, later on.
'awk' is 'domain specific'. The 'domain' being 'line based text streams', which means, that 'awk' sees the input as a stream of text lines, processes one by one, calling them 'records' and separating all the words within a line into 'fields'. Very square, very simple to understand. And you always can load the text into your editor or into a reading program. Or simply dump it to the terminal...
We have chose the GNU AWK implementation. This one comes even with a step-by-step debugger built in. This will help you a lot in understanding what went wrong in your program, should that be the case.
All in all, you should be able to learn basic programming within 10 hours, plus those hours, that you need to exercise. I would recommend:
One hour a day, reading the docs, with half an hour of practice after that and half an hour to an hour of exercise before you go to bed. After about two weeks you should be already quite competent. Just make sure, that you do not start learning the imperative part (that is, the 'AWK' language itself) before you did not understand 'awk' as a processor, which means, the declarative part.
Of course, you could go directly to Javascript. However, today's Javascript is a complex beast, bearing so many concepts and paradigms, that the learning curve is steep. If you know AWK, you already have flattened the Javascript learning curve a lot.
It's funny, that this topic came up yesterday, because it was just a few days ago, that I realized, how valuable of a didactic vessel GNU AWK (and its excellent documentation!) is for a newbie. As an experiment, I even started to port the AWK functions into a Javascript shim, so porting of pure AWK code becomes even easier!