LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-28-2010, 08:29 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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!
 
Old 02-28-2010, 08:46 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
See the link to Gawk in my sig. See here as well

Last edited by ghostdog74; 02-28-2010 at 08:48 PM.
 
Old 02-28-2010, 08:47 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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

Last edited by neonsignal; 02-28-2010 at 08:52 PM.
 
Old 02-28-2010, 09:02 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.
 
Old 02-28-2010, 09:12 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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.
 
Old 02-28-2010, 09:28 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jschiwal View Post
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
 
Old 03-01-2010, 05:54 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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?
 
Old 03-01-2010, 06:14 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 03-02-2010, 10:37 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 03-02-2010, 12:31 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by jschiwal View Post
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.
 
  


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
awk syntax wakatana Programming 10 10-20-2009 02:51 PM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 01:52 PM
passing a shell variable into awk - syntax for correct interpretation? johnpaulodonnell Programming 3 06-19-2007 01:13 PM
awk syntax to print particular record of xyz file? johnpaulodonnell Linux - Newbie 4 06-14-2007 08:47 AM


All times are GMT -5. The time now is 12:55 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration