http://goo.gl/EmKxy0

Home > ASE, Developement, Start Sybase, UNIX > Basic awk concepts

Basic awk concepts

Awk is basically a tool for text processing and report generation which is having features of programming language like C. Let see below how awk happens to be useful tool while writing shell scripts.

$ awk ‘{ print }’ /etc/passwd

When we executed awk, it evaluated the print command for each line in /etc/passwd, in order. All output is sent to stdout, and we get a result identical to catting /etc/passwd. Now, for an explanation of the { print } code block. In awk, curly braces are used to group blocks of code together, similar to C. Inside our block of code, we have a single print command. In awk, when a print command appears by itself, the full contents of the current line are printed.

$ awk ‘{ print $0 }’ /etc/passwd

In awk, the $0 variable represents the entire current line, so print and print $0 do exactly the same thing.

$ awk ‘{ print “” }’ /etc/passwd

Whenever you pass the “” string to the print command, it prints a blank line. If you test this script, you’ll find that awk outputs one blank line for every line in your /etc/passwd file. Again, this is because awk executes your script for every line in the input file. Here’s another example:

$ awk ‘{ print “hiya” }’ /etc/passwd

Print different fields – Fields are separated by colon “:”

  • ======================*

$ awk -F”:” ‘{ print $1 }’ /etc/passwd

$ awk -F”:” ‘{ print $1 ” ” $3 }’ /etc/passwd –> It will print first and third fields with a space between them.

 

[edit]========================

Reference: http://www.ibm.com/developerworks/linux/library/l-awk1/index.html

Check the Sybase Wiki @ sybasewiki.com
Categories: ASE, Developement, Start Sybase, UNIX Tags:
  1. No comments yet.
  1. No trackbacks yet.