skip to main | skip to sidebar

OPEN SOURCE

Linux for Better life

  • Home
  • Trik-Tips
  • Free Template
  • Blogger Hack
  • Free Ebook

UNIX and LINUX part 16


The following example matches any line containing the word Apples.
nawk '/Apples/' fruits

Escape Sequences
The following sequences of characters are referred to as escape sequences. Escape sequences specify a notation for characters that are not easily produced from your keyboard and/or cause problems when displayed to your screen. The nawk and echo commands use escape sequences. Although only nawk uses the escape sequences in regular expressions.
Sequence
Description
\b Matches a backspace.
\f Matches a form feed.
\n Matches a new-line.
\r Matches a carriage return.
\t Matches a tab.
\0ddd Matches the ASCII character for the given octal ASCII code.
\c Matches the literal character c. Use \ to generate a \.
Expression Patterns
Expression patterns perform number or string comparisons. Nawk provides eight comparison operators. Six of these operators are relational operators used to perform number or string comparison. The other two are string operators used to perform string comparisons.
String Operators may be used to compare a regular expression pattern to a specific field or the entire input line. The following two operators are supported:
Operator
Function
/RE/ Matches if the current line contains the specified regular expression. Same as the $0 ~ /RE/ command.
expr ~ RE Matches if the string value of expr contains the regular expression RE. For example,
nawk '$1 ~ /[Aa]pples/' fruits

matches lines where field 1 contains "Apples" or "apples."
expr !~ RE Matches if the string value of expr does not contain the regular expression expr. For example,
nawk '$3 !~ /[Aa]pples/' fruits

matches lines where field 1 does not contain the word "Apples" or "apples."
Relational Operators Relational operators compare strings and numbers. The following six operators are used to compare values.
Operator
Function
lv > rv True if lv is greater than rv. For example,
nawk 'NF > 3' fruits

prints each line containing more than three fields.
lv >= rv True if lv is greater than or equal to rv. For example,
nawk ' $1 >= "M" ' fruits

prints each line where the first field begins with a character that has a higher ASCII code than capital M. Such as N, O, P, Q, etc.
lv < rv True if lv is less than rv.
lv <= rv True if lv is less than or equal to rv.
lv == rv True if lv is equal to rv. For instance,
nawk '$1 == "Oranges" ' fruits

prints each line where the first field equals the string "Oranges."
lv != rv True if lv is not equal to rv.
Compound Patterns
A compound pattern is an expression consisting of multiple expression patterns combined with Boolean operators. The Boolean operators are || (OR), && (AND), ! (NOT), and use of parentheses for grouping the operators. If the result of a compound pattern is true, the current input line is matched and the associated action is executed.
Boolean Operators The Boolean operators can be used to combine regular and relational expressions. The following table describes the Boolean operators.
Operator
Function
! Negation. True if expression is NOT true. For example,
nawk '! /Apples/' fruits

prints each line that does not contain the string "Apples."
|| Logical OR. True if either expression is true. For instance,
nawk '$3 > "3,000,000" || $1 == "FRUIT"' fruits

prints the header line and each line with bushel counts of more than "3,000,000." Note 3,000,000 is a string, not a number, because of the commas.
&& Logical AND. True if both expressions are true. For example,
nawk '$3 > "3,000,000" && NF == 5' fruits

prints the header line and each line containing exactly three fields.
( ) Perform comparisons enclosed together(for grouping). For instance,
nawk '( $1 == "Apples" || $1 == "Oranges" ) &&
( NR == 4 )' fruits

prints lines where the first field is either "Apples" or "Oranges" and there are exactly four fields.
Range Patterns
Range patterns contain two patterns separated by a comma (,). A range pattern matches each line from the occurrence of the first pattern to the occurrence of the second pattern. Both patterns may appear on the same line, thus only the one line is matched. If no input line matches the first pattern, no lines are selected. If no input line matches the second pattern, all lines from the first pattern to the end of the file are selected. For example, the following command selects all of the lines from one that contains Apples to one that contains Cherries.
nawk '/Apples/,/Cherries/' fruits

ACTIONS
The action part of an nawk program performs specified actions when a pattern is matched. If no action is specified the default print action is used. The default is the same as specifying { print }, which is equivalent to { print $0 }.
Comments
The nawk command ignores any characters on a line following a # sign. Thus you can document your nawk programs using the number sign.
Constants
Two types of constants are used by nawk, numeric and string. String constants are created by enclosing a sequence of characters in double or single quote marks. For example,
$1 == "FRUIT"

Posted by MALIK

Labels: BSD, code, command, program, unix

Newer Post Older Post 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)
    • ► 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)
      • UNIX and LINUX part 16
      • UNIX and LINUX part 15
      • UNIX and LINUX part 14
      • UNIX and LINUX part 13
      • UNIX and LINUX part 12
      • UNIX and LINUX part 11
      • UNIX and LINUX part 10
      • UNIX and LINUX part 9
      • UNIX and LINUX part 8
      • UNIX and LINUX part 7
      • UNIX and LINUX part 7
      • UNIX and LINUX part 6
      • UNIX and LINUX part 5
      • UNIX and LINUX part 4
      • UNIX and LINUX part 3
      • UNIX and LINUX part 2
      • UNIX and LINUX part 1
      • A SAMPLE SESSION WITH UNIX USER COMMANDS
    • ► 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