skip to main | skip to sidebar

OPEN SOURCE

Linux for Better life

  • Home
  • Trik-Tips
  • Free Template
  • Blogger Hack
  • Free Ebook
Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

UNIX and LINUX part 15


If an example is given for syntax reasons only, the filename infile is used.
PROGRAMS
The nawk command interprets a set of commands known as the nawk program. The program can be specified as an argument on the shell command line or stored in a file. In either case the program consists of one or more pattern-action statements. The general form of the pattern-action statement is:
pattern { action }

For example,
nawk 'NF < 5 { print $1 }' fruits

prints the first field of a line if less than five fields exist on the line. Fields are space or tab separated words. The built-in variable NF contains the number of fields in the current line.
Nawk reads each line of input automatically. If the pattern matches the input line (record) in part or whole, then the following action is performed. If no pattern is specified, then the action is performed for every input record. The action may consist of multiple programming statements discussed later. The action must be enclosed by '{}' for nawk to function properly. The general format is:
{ action }

For example,
nawk '{ print }' fruits

prints every line of the file infile. Thus you wrote a very simple version of the cat command. This is the default pattern-action statement of nawk.
If no action is specified, each matching input record is displayed on the standard output. The general format is:
pattern

For example,
nawk 'NF == 4' fruits

prints each line consisting of four fields.
WRITING PROGRAMS
It is very common to write small nawk programs in-line; that is, on the command line or even spread over a couple of lines. For example,
nawk 'BEGIN { print "Annual Fruit Report" }
{ print }
END { print "More fruit to Grow" }' fruits

prints the banner line Annual Fruit Report, every line of the fruits file, and a trailer line More fruit to Grow. This line can be typed from your terminal or placed in a shell script.
NOTE:
In-line nawk programs are limited in length to the command line buffer of your system. This buffer varies; some systems are set to 512 characters of information while others are set to 10240 characters. The general rule of thumb for the length of in-line programs is that they should never exceed 2000 characters (approximately one screenful of characters).
The same nawk program written in a file format would be invoked by the command line:
nawk -f fruitprog fruits

The fruitprog file would contain the following lines.
BEGIN { print "Annual Fruit Report" }
{ print }
END { print "More fruit to Grow" }

Notice the single quotes are removed; the nawk command itself is not needed for the input files.
PATTERNS
Patterns are used to select lines from the input. If a pattern matches a string on an input line, the associated action is performed. It is possible to write an nawk program that does not have a pattern, such as
nawk '{ print $1 }' fruits

The default pattern selects all lines from the input.
NOTE:
The BEGIN and END patterns both require an action.
The BEGIN Pattern
The BEGIN pattern does not match any input. Instead, the action associated with the BEGIN pattern is executed before any input is read by nawk. This allows for initialization of variables, the printing of headers, and other coding that needs to be done before the first line of input is read by nawk. The general format is:
nawk 'BEGIN { initialization code; headers; etc. }' infile

The following example prints a header line.
nawk 'BEGIN { print "Fruit Shipped From Primary State" }' fruits

The END pattern
The END pattern does not match any input. Instead, the action associated with the END pattern is executed after all of the input has been read by nawk. The general format is:
nawk 'END { wrap up code }' infile

The following example prints the total number of lines read by nawk.
nawk 'END { print NR }' infile

The built-in variable NR contains the Number of Records read.
Regular Expression Patterns
The following table contains each regular expression and the task that it performs when used inside a pattern. Regular Expressions are often referred to as REs in UNIX terminology. Thus we use the RE notation for uniformity and briefness.
Metacharacters
Metacharacters are the special characters used in regular expression patterns that have special meanings. Metacharacters are often referred to as special or magic characters.
Regular expression patterns must be enclosed in slashes. For example, to match any records (lines) containing the string cpu you would use /cpu/ as the pattern. The string inside the slashes may be any valid combination of the following list.
Special RE
Description
c Matches the character c if c is not a special regular expression character.
\ Escapes the meaning of a metacharacter.
^ Matches the beginning of the line.
$ Matches the end of the line.
. Matches any single character other than the new-line.
[class] A character class. Matches any one character in the class.
[c1-c2] Matches any one of the ASCII characters in the range defined within the brackets.
[^class] Does NOT match any of the ASCII characters listed within the brackets. Ranges may be specified.
| Alternation of regular expressions. Matches either one or the other of the regular expressions provided.
(!) Concatenation operation. Normally the parentheses are omitted. Allows for control of precedence in the interpretation of the regular expressions specified.
RE* Matches zero or more occurrences of the preceding regular expression.
RE+ Matches one or more occurrences of the preceding regular expression.
RE? Matches zero or one occurrence of the preceding regular expression.
// The null RE refers to the last RE defined.

Posted by MALIK

Labels: BSD, c programming, command, program, unix

UNIX and LINUX part 14


Module 6
awk/nawk
DESCRIPTION
The external awk program is an interpretive programming language. Interpretive means awk executes the program as it reads the code, unlike the C programming language which must be compiled before it can be executed. Awk is somewhat of a cross between the egrep command and the C programming language. Like egrep it searches for a regular expression in the input. Like C it can be programmed to perform almost any data manipulation processing.
NOTE:
The new version of awk is named nawk (new awk). Some vendors provide only the old version of awk, some provide nawk under the name of awk, while others provide awk and nawk. System V Release 4.0 provides awk and nawk. It is advisable to use nawk unless of course you have to consider portability to systems that do not support nawk. This module describes the nawk program.
Nawk lends itself well to generating reports, transforming data, retrieving information, and validating data. It is designed to handle data in rows and columns, but it is also useful in locating specific data within free formatted data streams. Its features include:
* Structures, operators, and syntax resembling the C programming language
* Regular expression pattern comparison
* Multisource input. It can read from files, pipes, and the keyboard in the same program. It can be interactive.
* Multidestination output. It can write to multiple files, pipes, and the terminal.
* Formatted output using the C printf function
* Automatically declares typeless variables (a variable may be string or number)
* Automatically parses input lines into fields (columns) and records (lines)
COMMAND FORMAT
Following is the general format of the nawk command.
nawk [ -Fs ] program [ VAR=VAL ] [ file_list ] [ - ]
nawk [ -Fs ] -f prog_file [ VAR=VAL ] [ file_list ] [ - ]

Options
The following list describes the options and their arguments that may be used to control how nawk functions.
-Fs Set field separator to regular expression s; default is white space (spaces and/or tabs).
-f prog_file The -f informs nawk that the next argument is the name of the file containing the nawk commands. The file prog_file contains the nawk program (commands). This allows you to write large nawk programs without exceeding the shell's or nawk's input buffer limit on the command line. The input buffer on most systems is between 512 and 10240 bytes. To find out your system's limit, type grep "ARG_MAX" /usr/include/limits.h and press Return. On BSD systems, type egrep "NCARGS" /usr/include/sys/param.h and press Return.
Arguments
The following list describes the arguments that may be passed to the nawk command.
'program' A single argument containing the nawk program (commands). The argument should be enclosed in single quotes to keep the shell from interpreting it.
VAR=VAL Command line variables. These variables are passed to the nawk program for internal use.
file_list One or more files containing data to be scanned and processed by nawk.
- The standard input is read as input. This may be intermixed with files in the file_list.
If no files are specified for input, nawk reads from the standard input.
DATA FILE
The examples throughout this module assume you have a file named fruits in your HOME directory. The file should contain the following data.
FRUIT STATE BUSHELS PRICE STATUS
Apples Washington 6,700,000 10.59
Oranges Florida 5,900,000 11.69
Peaches Texas 3,600,000 13.79 Incomplete
Pears California 3,100,000 12.89
Raisins California 2,300,000 15.00
Cherries Missouri 2,100,000 17.49
Pineapple Hawaii 3,900,000 12.99
Coconuts Hawaii 2,600,000 14.39 Incomplete

Posted by MALIK

Labels: advisable, awk, BSD, c programming, command, nawk

Older Posts Home

Links

  • ALJAZEERA TV
  • ANTARA
  • Bali`s Blogger
  • BBC
  • BERITANET.COM
  • CHIP ONLINE
  • CNN
  • DETIK.COM
  • E-BOOK
  • Economic
  • Friends
  • Friendster's Blog
  • Habibieafsyah
  • HARVARD UNIVERSITY
  • ILMU KOMPUTER
  • INFO LINUX
  • KICK ANDY
  • LINUX OpenSuse
  • LINUX SLAX
  • Linux Temanggung
  • LINUX UBUNTU
  • LIPUTAN 6 SCTV
  • MASTER BLOGGER
  • METROTV NEWS
  • MUSIC DOWNLOAD
  • My Class
  • Robotic AMIKOM
  • SMA N 3 TEMANGGUNG
  • SMART E-LEARNING
  • SOFTPEDIA
  • STMIK AMIKOM
  • SUPPORTED BLOGGER
  • TIPS TRICKS
  • TRIAL FILM
  • TV ONE ONLINE

Blog Archive

  • ▼ 2009 (38)
    • ▼ 05/31 - 06/07 (1)
      • Java
    • ► 02/15 - 02/22 (1)
    • ► 01/11 - 01/18 (11)
    • ► 01/04 - 01/11 (25)
  • ► 2008 (70)
    • ► 11/30 - 12/07 (22)
    • ► 11/16 - 11/23 (18)
    • ► 11/09 - 11/16 (1)
    • ► 10/26 - 11/02 (5)
    • ► 10/19 - 10/26 (16)
    • ► 10/12 - 10/19 (4)
    • ► 10/05 - 10/12 (4)



$100.00 Anda pasti untung setelah registrasi!






Search Engine Optimization and SEO Tools







Bookmark CO.CC:Free Domain