LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   AWK Syntax (https://www.linuxquestions.org/questions/programming-9/awk-syntax-792227/)

MTK358 02-28-2010 07:29 PM

AWK Syntax
 
I am trying to learn AWK and I checked out a few tutorials and they say what you can do with it, but I just can't find out the syntax! I just don't understand it and I can't find an explanation of it anywhere!

ghostdog74 02-28-2010 07:46 PM

See the link to Gawk in my sig. See here as well

neonsignal 02-28-2010 07:47 PM

Here is the introductory section from 'man awk':

Quote:

An AWK program is a sequence of pattern {action} pairs and user function definitions.

A pattern can be:
  • BEGIN
  • END
  • expression
  • expression , expression

One, but not both, of pattern {action} can be omitted. If {action} is omitted it is implicitly { print }. If pattern is omitted, then it is implicitly matched. BEGIN and END patterns require an action.

Statements are terminated by newlines, semi-colons or both. Groups of statements such as actions or loop bodies are blocked via { ... } as in C. The last statement in a block doesn't need a terminator. Blank lines have no meaning; an empty statement is terminated with a semi-colon. Long statements can be continued with a backslash, \. A statement can be broken without a backslash after a comma, left brace, &&, ||, do, else, the right parenthesis of an if, while or for statement, and the right parenthesis of a function definition. A comment starts with # and extends to, but does not include the end of line.

The following statements control program flow inside blocks.
  • if ( expr ) statement
  • if ( expr ) statement else statement
  • while ( expr ) statement
  • do statement while ( expr )
  • for ( opt_expr ; opt_expr ; opt_expr ) statement
  • for ( var in array ) statement
  • continue
  • break


jschiwal 02-28-2010 08:02 PM

For awk, if you install the source and run:
./configure
make pdf

This will generate the book "GAWK: Effective Awk Programming". It might be available as "gawk-doc" in a separate package separately.
This book is excellent. Having the source, you can cut and past the two sample database text files `BBS-List' and 'inventory-shipped' from the source gawk.texi file and save them to a file. That makes it easier to try out the examples yourself.

The O'Reilly book "Sed & Awk" is very good a well.

The sed info document pales by comparison to the gawk document, unfortunately.

MTK358 02-28-2010 08:12 PM

I keep hearing it's like C, so I decided just to try to use it like C but without ';'s on the end of commands, and I am starting to get the hang of it.

I will check out the links and recommendations tomorrow.

BTW:

Quote:

Originally Posted by jschiwal
man -Tps <topic> | tee ~/Documents/<topic>.ps | okular -

Pretty neat, I actually made a little script that does that (without the 'tee', though) and put in in ~/bin.

ghostdog74 02-28-2010 08:28 PM

Quote:

Originally Posted by jschiwal (Post 3880497)
For awk, if you install the source and run:
./configure
make pdf

This will generate the book "GAWK: Effective Awk Programming". It might be available as "gawk-doc" in a separate package separately.
This book is excellent. Having the source, you can cut and past the two sample database text files `BBS-List' and 'inventory-shipped' from the source gawk.texi file and save them to a file. That makes it easier to try out the examples yourself.

why this trouble? :). its here

MTK358 03-01-2010 04:54 AM

I'm pretty sure I understand it now.

Anyway, I was wondering if it is possible to do search/replace in awk, like sed's 's' command?

ghostdog74 03-01-2010 05:14 AM

Quote:

Originally Posted by MTK358 (Post 3880800)
I'm pretty sure I understand it now.

Anyway, I was wondering if it is possible to do search/replace in awk, like sed's 's' command?

have you read the gawk manual page (in my sig) yet? the function to do replacement is documented there.

jschiwal 03-02-2010 09:37 AM

Quote:

Originally Posted by MTK358 (Post 3880500)
Pretty neat, I actually made a little script that does that (without the 'tee', though) and put in in ~/bin.

Actually, so do I. Without the tee as well. I was just getting bored with it and added it in.

MTK358 03-02-2010 11:31 AM

Quote:

Originally Posted by jschiwal (Post 3882670)
Actually, so do I. Without the tee as well. I was just getting bored with it and added it in.

It's more comfortable for those man pages that are more like tutorials.


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