Friday, March 9, 2012

Linux Interview Questions Part 3

Q:What is the use of AWK in linux and explain it in detail ?
A: Awk is a programming language which allows easy manipulation of structured data and the generation of formatted reports.The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions.

Some of the key features of Awk are:
1)Awk views a text file as records and fields.
2)Like common programming language, Awk has variables, conditionals and loops
3)Awk has arithmetic and string operators.
4)Awk can generate formatted reports

Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files

Syntax:
wk '/search pattern1/ {Actions}

a /search pattern2/ {Actions}' file


In the above awk syntax:
search pattern is a regular expression.
Actions – statement(s) to be performed.
several patterns and actions are possible in Awk.
file – Input file.
Single quotes around program is to avoid shell not to interpret any of its special characters

Awk Working Methodology :
1)Awk reads the input files one line at a time.
2)For each line, it matches with given pattern in the given order, if matches performs the corresponding action.
3)If no pattern matches, no action will be performed.
4)In the above syntax, either search pattern or action are optional, But not both.
5)If the search pattern is not given, then Awk performs the given actions for each line of the input.
6)If the action is not given, print all that lines that matches with the given patterns which is the default action.
7)Empty braces with out any action does nothing. It wont perform default printing operation.
8)Each statement in Actions should be delimited by semicolon.

No comments:

Post a Comment