SlideShare une entreprise Scribd logo
1  sur  41
awk – Essentials and Examples
              1

       Logan Palanisamy
Agenda
                            2

 Elements of awk
 Optional Bio Break
 Examples and one-liners
 Q&A
What is awk
                          3

 An acronym of the last names of the three authors
 General purpose pattern-scanning and processing
  language
 Used for filtering, transforming and reporting
 More advanced than sed, but less complicated than
  C; less cryptic than Perl.
 gawk, nawk
awk syntax
                             4

 awk [-Ffield_sep] 'cmd' infile(s)
 awk [-Ffield_sep] –f cmd_file infile(s)
 infile can be the output of pipeline.
 Space is the default field_sep
awk mechanics
                             5

 [pattern] [{action} …]
 Input files processed a line at a time
 Every line is processed if there is no pattern
 Lines split into fields based on field-sep
 Print is the default action.
 Input files not affected in anyway
Field Names
                             6

 Lines split into fields based on field-sep
 $0 represents the whole line
 $1, $2, … $n represent different fields
 Field names could be used as variables
Built-in variables.
                                        7

Variable    Explanation
FS          Field separator variable for input lines. Defaults to space or tab
NR          Number of input lines processed so far
NF          Number of fields in the current input line
FILENAME Name of the current input file
OFMT        Default format for output numbers
OFS         Output field separator. Defaults to space
ORS         Output record separator. Defaults to new-line character
RS          Input Record Separator. Defaults to new-line character.
FNR         Same as NR; but gets reset after each file unlike NR
RSTART,     Variables set by the match() function which indicates where the
RLENGTH     match starts and how long the match is
SUBSEP      Subscript separator. Used in multi-dimensional arrays
Operators
                                       8

Operator       Explanation
+, -, *, /     Addition, Subtraction, Multiplication, Division,
%              Remainder/Modulo operation
++             Unary increment (var++ same as var=var+1)
--             Unary decrement
^ or **        Exponentaion
+=, -=, *=, /=, Assignment operator preceded by arithmetic operation (var+=5
%=              same as var=var+5)
No operator    String concatenation (newstr=“new” $3)
?:             Ternary operator (expr1 ? expr2 : expr3)
Relational Operators.
                             9

Operator      Explanation
==            Equality operator
!=            Not equal to
<             Less than
<=            Less than or equal to
>             Greater than
>=            Greater than equal to
~             Contains regular expression
!~            Doesn‟t contain regular expression
awk patterns
                           10

 Can match either particular lines or ranges of lines
 Regular expression patterns
 Relational expression patterns
 BEGIN and END patterns
Regular Expressions
                                    11

Meta character Meaning
.             Matches any single character except newline
*             Matches zero or more of the character preceding it
              e.g.: bugs*, table.*
^             Denotes the beginning of the line. ^A denotes lines starting
              with A
$             Denotes the end of the line. :$ denotes lines ending with :
             Escape character (., *, [, , etc)
[]            matches one or more characters within the brackets. e.g.
              [aeiou], [a-z], [a-zA-Z], [0-9], [[:alpha:]], [a-z?,!]
[^]           matches any characters others than the ones inside brackets.
              eg. ^[^13579] denotes all lines not starting with odd numbers,
              [^02468]$ denotes all lines not ending with even numbers
<, >        Matches characters at the beginning or end of words
Extended Regular Expressions
                                   12

Meta character Meaning
|             alternation. e.g.: ho(use|me), the(y|m), (they|them)
+             one or more occurrences of previous character. a+ is same as
              aa*)
?             zero or one occurrences of previous character.
{n}           exactly n repetitions of the previous char or group
{n,}          n or more repetitions of the previous char or group
{n, m}        n to m repetitions of previous char or group. For the above
              four –re-interval option needs to be specified
(....)        Used for grouping
Regular Expressions – Examples
                                     13

Example                       Meaning
.{10,}                        10 or more characters. Curly braces have to
                              escaped
[0-9]{3}-[0-9]{2}-[0-9]{4}    Social Security number
([2-9][0-9]{2})[0-9]{3}-[0-   Phone number (xxx)yyy-zzzz
9]{4}
[0-9]{3}[ ]*[0-9]{3}          Postal code in India
[0-9]{5}(-[0-9]{4})?          US ZIP Code with optional four-digit extension
Regular Expression Patterns.
                                          14

Example                       Explanation
awk „/pat1/‟ infile           Same as grep „pat1‟ infile
awk „/pat1/, /pat2/‟ infile   Print all lines between pat1 and pat2 repetitively
awk „/pat1|pat2/‟ infile      Print lines that have either pat1 or pat2
awk „/pat1.*pat2/‟ infile     Print lines that have pat1 followed by pat2 with
                              something or nothing in between
Relational Expression Patterns.
                                         15

Example                     Explanation
awk „$1==“USA”‟ infile      Print the line if the first field is USA
awk „$2 !=“xyz”‟ infile     Print all lines whose second field is not “xyz”
awk „$2 < $3‟ infile        Print all lines whose third field is greater than the
                            second
awk „$5 ~ /USA/‟ infile     Print if the fifth field contains USA
awk „$5 !~ /USA/‟ infile    Print if the fifth field doesn‟t contain USA
awk „NF == 5‟ infile        Print lines that have five fields
awk „NR == 5, NR==10‟       Print lines 5 to 10
infile
awk „NR%5==0‟ infile        Print every fifth line (% is the modulo operator)
awk „NR%5‟ infile           Print everything other than every fifth line
awk „$NF ~ /pat1/‟ infile   Print if the last field contains pat1
awk compound-patterns
                            16

 Compound patterns formed with Boolean operations
    (&&, ||, !), and range patterns
   pat1 && pat2 (compound AND)
   pat1 || pat2 (compound OR)
   !pat1 (Negation)
   pat1, pat2 (range pattern)
Compound Pattern Examples
                                      17

Example                             Explanation
awk „/pat1/ && $1==“str1”‟ infile   Print lines that have pat1 and whose first
                                    field equals str1
awk „/pat1/ || $2 >= 10‟ infile     Print lines that have pat1 OR whose second
                                    field is greater than or equal to 10
awk „!/pat1/‟ infile                Same as grep –v “pat1” infile
awk „NF >=3 && NF <=6‟ infile       Print lines that have between 3 and six
                                    fields
awk „/pat1/ || /pat2/‟ infile       Same as awk „/pat1|pat2/‟ infile
awk „/pat1/, /pat2/‟ infile         Print all lines between pat1 and pat2
                                    repetitively
awk „!/pat1|pat2/‟ infile           Print lines that have neither pat1 nor pat2
awk „NR > 30 && $1 ~ /pat1|pat2/‟   Print lines beyond 30 that have first field
infile                              containing either pat1 or pat2
Compound Pattern Examples
                                     18

Example                            Explanation
awk „/pat1/&&/pat2/‟ infile        Print lines that have both pat1 and pat2.
awk „/pat1.*pat2/‟ infile          How is this different from the one above?
awk „NR<10 || NR>20‟ infile        Print all lines except lines 10 to 20
awk „!(NR >=10 && NR<=20)‟ infile Print lines between 10 and 20. Same as awk
                                  ‘NR==10, NR==20’ infile
BEGIN and END patterns
                            19

 BEGIN allows actions before any lines are processed.
 END allows actions after all lines have been
  processed
 Either or both optional
     BEGIN {action}
     [Pattern] {action}
     END {action}
BEGIN
                                 20

 Use BEGIN to:
   Set initial values for variables

   Print headings

   Set internal field separator (same as –F on command line)
    awk „BEGIN {FS=“:”; print “File name”, FILENAME}‟ file2 file2
END
                                   21

 Use END to:
   Perform any final calculations

   Print report footers.

   Do any thing that must be done after all lines have been
    processed.
    awk „END {print NR}‟ file2 file2
Creating Actions
                                22

 Actions consist of one or more statements separated
  by semicolon, newline, or a right-brace.
 Types of statements:
    Assignment statement (e.g.var1=1)
    Flow-control statements
    Print control statement
Flow-control statements
                                       23

Statement                   Explanation
if (conditional)            Perform statement_list1 if conditional is true.
{statement_list1}           Otherwise statement_list2 if specified
[else {statement_listt2}]
while (conditional)         Perform statement_list while conditional is true
{statement_list}
for                          Perform int_expr firt. While conditional_expr is
(int_expr;conditional_expr true, perform statement_list and execute ctrl_expr.
;ctrl_expr) {statement_list}
break                       Break from the containing loop and continue with
                            the next statement
continue                    Go to the next iteration of the containing loop without
                            executing the remaining statements in loop
next                        Skip remaining patterns on this line
exit                        Skip the rest of the input and go to the END pattern
                            if one exists or exit.
Print-control statements
                                    24

Statement                 Explanation
print [expression_list]   Print the expression on stdout unless redirected to
[>filename]               filename.
printf format [,          Prints the output as specified in format (like printf
expression_list]          in C). Has a rich set of format specifiers.
[>filename]
Variables
                            25

 Provide power and flexibility
 Formed with letters, numbers and underscore
    character.
   Can be of either string or numeric type
   No need to declare or initialize.
   Type implied by the assignment. No $ in front of
    variables. (e.g. var1=10; job_type=„clerk‟)
   Field names ($1, $2, ..$n) are special form of
    variables. Can be used like any other variable.
Arrays
                          26

 One-dimensional arrays: array_name[index]
 Index can be either numeric or string. Starts with 1
  if numeric
 No special declaration needed. Simply assign
  values to an array element.
 No set size. Limited only by the amount of memory
  on the machine.
  phone[“home”],  phone[“mobile”], phone[var1],
   phone[$1], ranks[1]
Multi-Dimensional arrays
                                27

 Arrays are one-dimensional.
 Array_name[1,2] not supported
 Concatenate the subscripts to form a string which
 could be used as the index:
     array_name[1”,”2]
   Space is the concatenation operator. “1,2”, a three character
    string is the index.
 Use SUBSEP, subscript separator, variable to
 eliminate the need to have double quotes around
 the comma.
Built-in functions
                                                28

Function                           Explanation
cos(awk_expr)                      Cosine of awk_expr
exp(awk_expr)                      Returns the exponential of awk_expr (as in e raised to the
                                   power of awk_expr)
index(str1, str2)                  Returns the position of strt2 in str1.
length(str)                        Returns the length of str
log(awk_expr)                      Base-e log of awk_expr
sin(awk_expr)                      Sine of awk_expr
sprintf(frmt, awk_expr)            Returns the value of awk_expr formatted as per frmt
sqrt(awk_expr)                     Square root of awk_expr
split(str, array, [field_sep])     Splits a string into its elements and stores into an array
substr(str, start, length)         Returns a substring of str starting at position “start” for
                                   “length” characters.
toupper(), tolower()               Useful when doing case-insensitive searches
Built-in functions contd.
                                            29

Function                       Explanation
sub(pat1, “pat2”, [string])    Substitute the first occurrence of pat1 with pat2 in string.
                               String by default is the entire line
gsub(pat1, “pat2”, [string])   Same as above, but replace all occurrences of pat1 with
                               pat2.
match(string, pat1)            Finds the regular expression pat1, and sets two special
                               variables (RSTART, RLENGTH) that indicate where the
                               regular expression begins and ends
systime()                      returns the current time of day as the number of seconds
                               since Midnight, January 1, 1970
Case Insensitive Match
                            30

 Case insensitive match:
 awk „BEGIN {ignorecase=1} /PAT1/‟
 awk „tolower($0) ~ /pat1/ …‟
User-Defined functions
                                                                           31

Gawk allows user defined functions

#!/usr/bin/gawk -f
{
     if (NF != 4) {
                 error("Expected 4 fields");
     } else {
                 print;
     }
}
function error ( message ) {
     if (FILENAME != "-") {
                 printf("%s: ", FILENAME) > "/dev/tty";
     }
     printf("line # %d, %s, line: %sn", NR, message, $0) >> "/dev/tty";
}
Very Simple Examples
                            32

 Find the average filesize in a directory
 Find the users without password
 Convert String to Word (string2word.awk)
 List the file count and size for each user
 (cnt_and_size.awk)
Awk one-liners
                                              33

Example                            Explanation
awk‟{print $NF}‟ infile            Print the last field in each line
awk‟{print $(NF-1)}‟ infile        Print the field before the last field. What would
                                   happen if () are removed? What happens if there is
                                   only one field
awk‟NF‟ infile                     Print only non-blank lines. Same as awk „/./‟
awk „{print length, $0)‟ infile    Print each line preceded by its length.
awk „BEGIN {while                  Print 1 to 10
(++x<11) print x}‟
awk „BEGIN {for (i=10;             Print 10 to 50 in increments of 4
i<=50; i+=4) print i}‟
awk „{print; print “”}‟ infile     Add a blank line after every line
awk „{print; if (NF>0) print       Add a blank line after every non-blank line
“”}‟ infile
Awk one-liners
                                           34

Example                       Explanation
awk‟NF !=0 {++cnt} END        Count the number of non-blank lines
{print cnt}‟ infile
ls –l | awk „NR>1 {s+=$5}     Return the average file size in a directory
END {print “Average:”
s/(NR-1)}‟
awk „/pat1/?/pat2/:/pat3/‟    uses ternary operator ?: Equivalent to awk „/pat1/ && /pat2/ ||
                              pat3‟ except for lines containing both pat1 and pat3
infile
awk „NF<10?/pat1/:/pat2/‟     Use pat1 if number of fields is less than 10;
infile                        otherwise use pat2
awk „ORS=NR%3?” ”:”n”‟       Join three adjacent lines. ORS is the output record
infile                        separator
awk „ORS=NR%3?”t”:”n”       Print the first field three to row. ORS is the output
{print $1}‟ infile            record separator
awk „FNR < 11‟ f1, f2, f3     Concatenate the first 10 lines of f1, f2, and f3.
Awk one-liners
                                        35

Example                       Explanation
awk „length < 81‟             Print lines that are shorter than 81 characters
awk „/pat1/, 0‟               Print all lines between the line containing pat1 and
                              end of file
awk „NR==10, 0‟               Print lines 10 to the end of file. The end condition
                              “0” represents “false”.
awk '{ sub(/^[ t]+/, "");    Trim the leading tabs or spaces. Called ltrim
print }'
awk '{ sub(/[ t]+$/, "");    Trim the trailing tabs or spaces. Called rtrim
print }'
awk '{ gsub(/^[ t]+|[        Trim the white spaces on both sides
t]+$/, ""); print }'
Awk one-liners
                                         36

Example                       Explanation
awk '/pat1/ { gsub(/pat2/,    Replace pat2 with “str” on lines containing pat1
“str") }; { print }'
awk '{ $NF = ""; print }'     Delete the last field on each line
Awk one-liners
                        37

 http://www.catonmat.net/blog/awk-one-liners-
 explained-part-one/
Translators
                         38

 awk2c – Translates awk programs to C
 awk2p – Translates awk progrms to Perl
References
                        39

 sed & awk by Dale Dougherty & Arnold Robins
 http://www.grymoire.com/Unix/Awk.html
 http://www.vectorsite.net/tsawk.html
 http://snap.nlc.dcccd.edu/reference/awkref/gawk
 _toc.html
Q&A
                            40

 devel-awk@yahoo-inc.com
Unanswered questions
                            41

 How to print lines that are outside a block of lines?
  (print lines that are not enclosed by /pat1/,/pat2/
 Does awk support grouping and back-referencing
  (e.g. identify adjacent duplicate words)?

Contenu connexe

Tendances (19)

C reference card
C reference cardC reference card
C reference card
 
C Reference Card (Ansi) 2
C Reference Card (Ansi) 2C Reference Card (Ansi) 2
C Reference Card (Ansi) 2
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Linux intro 5 extra: awk
Linux intro 5 extra: awkLinux intro 5 extra: awk
Linux intro 5 extra: awk
 
strings
stringsstrings
strings
 
Functions
FunctionsFunctions
Functions
 
Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Grep
GrepGrep
Grep
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
ruby3_6up
ruby3_6upruby3_6up
ruby3_6up
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
 
newperl5
newperl5newperl5
newperl5
 

En vedette

Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacksshekarkcb
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duoJoshua Thijssen
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processingAnton Arhipov
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line toolsEric Wilson
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With ArgumentsAlex Shaw III
 
Testingtechniques And Strategy
Testingtechniques And StrategyTestingtechniques And Strategy
Testingtechniques And Strategynazeer pasha
 
Awk Unix Utility Explained
Awk Unix Utility ExplainedAwk Unix Utility Explained
Awk Unix Utility ExplainedPeter Krumins
 
Linux 101 Exploring Linux OS
Linux 101 Exploring Linux OSLinux 101 Exploring Linux OS
Linux 101 Exploring Linux OSRodel Barcenas
 
unix crontab basics
unix crontab basicsunix crontab basics
unix crontab basicssaratsandhya
 
Presentation of awk
Presentation of awkPresentation of awk
Presentation of awkyogesh4589
 
Sed Unix Utility Explained
Sed Unix Utility ExplainedSed Unix Utility Explained
Sed Unix Utility ExplainedPeter Krumins
 

En vedette (20)

Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacks
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duo
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processing
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With Arguments
 
Unix day3 v1.3
Unix day3 v1.3Unix day3 v1.3
Unix day3 v1.3
 
Unix day4 v1.3
Unix day4 v1.3Unix day4 v1.3
Unix day4 v1.3
 
Unix
UnixUnix
Unix
 
Unix day2 v1.3
Unix day2 v1.3Unix day2 v1.3
Unix day2 v1.3
 
What Linux is what you should also have on your computer.
What Linux is what you should also have on your computer.What Linux is what you should also have on your computer.
What Linux is what you should also have on your computer.
 
Testingtechniques And Strategy
Testingtechniques And StrategyTestingtechniques And Strategy
Testingtechniques And Strategy
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
Awk Unix Utility Explained
Awk Unix Utility ExplainedAwk Unix Utility Explained
Awk Unix Utility Explained
 
Linux 101 Exploring Linux OS
Linux 101 Exploring Linux OSLinux 101 Exploring Linux OS
Linux 101 Exploring Linux OS
 
unix crontab basics
unix crontab basicsunix crontab basics
unix crontab basics
 
Presentation of awk
Presentation of awkPresentation of awk
Presentation of awk
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Sed Unix Utility Explained
Sed Unix Utility ExplainedSed Unix Utility Explained
Sed Unix Utility Explained
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Chap06
Chap06Chap06
Chap06
 

Similaire à Awk essentials

awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminZoumanaDiomande1
 
Awk A Pattern Scanning And Processing Language
Awk   A Pattern Scanning And Processing LanguageAwk   A Pattern Scanning And Processing Language
Awk A Pattern Scanning And Processing LanguageCrystal Sanchez
 
Awk --- A Pattern Scanning And Processing Language (Second Edition)
Awk --- A Pattern Scanning And Processing Language (Second Edition)Awk --- A Pattern Scanning And Processing Language (Second Edition)
Awk --- A Pattern Scanning And Processing Language (Second Edition)Brooke Heidt
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashessana mateen
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Beginning with vi text editor
Beginning with vi text editorBeginning with vi text editor
Beginning with vi text editorJose Pla
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxmecklenburgstrelitzh
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 
Regular expression
Regular expressionRegular expression
Regular expressionRajon
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexKasper de Waard
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheetMartin Cabrera
 

Similaire à Awk essentials (20)

awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
 
Awk A Pattern Scanning And Processing Language
Awk   A Pattern Scanning And Processing LanguageAwk   A Pattern Scanning And Processing Language
Awk A Pattern Scanning And Processing Language
 
Awk --- A Pattern Scanning And Processing Language (Second Edition)
Awk --- A Pattern Scanning And Processing Language (Second Edition)Awk --- A Pattern Scanning And Processing Language (Second Edition)
Awk --- A Pattern Scanning And Processing Language (Second Edition)
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Regexp
RegexpRegexp
Regexp
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Beginning with vi text editor
Beginning with vi text editorBeginning with vi text editor
Beginning with vi text editor
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
 
Ch2
Ch2Ch2
Ch2
 
newperl5
newperl5newperl5
newperl5
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
RegexCat
RegexCatRegexCat
RegexCat
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 

Dernier

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Dernier (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Awk essentials

  • 1. awk – Essentials and Examples 1 Logan Palanisamy
  • 2. Agenda 2  Elements of awk  Optional Bio Break  Examples and one-liners  Q&A
  • 3. What is awk 3  An acronym of the last names of the three authors  General purpose pattern-scanning and processing language  Used for filtering, transforming and reporting  More advanced than sed, but less complicated than C; less cryptic than Perl.  gawk, nawk
  • 4. awk syntax 4  awk [-Ffield_sep] 'cmd' infile(s)  awk [-Ffield_sep] –f cmd_file infile(s)  infile can be the output of pipeline.  Space is the default field_sep
  • 5. awk mechanics 5  [pattern] [{action} …]  Input files processed a line at a time  Every line is processed if there is no pattern  Lines split into fields based on field-sep  Print is the default action.  Input files not affected in anyway
  • 6. Field Names 6  Lines split into fields based on field-sep  $0 represents the whole line  $1, $2, … $n represent different fields  Field names could be used as variables
  • 7. Built-in variables. 7 Variable Explanation FS Field separator variable for input lines. Defaults to space or tab NR Number of input lines processed so far NF Number of fields in the current input line FILENAME Name of the current input file OFMT Default format for output numbers OFS Output field separator. Defaults to space ORS Output record separator. Defaults to new-line character RS Input Record Separator. Defaults to new-line character. FNR Same as NR; but gets reset after each file unlike NR RSTART, Variables set by the match() function which indicates where the RLENGTH match starts and how long the match is SUBSEP Subscript separator. Used in multi-dimensional arrays
  • 8. Operators 8 Operator Explanation +, -, *, / Addition, Subtraction, Multiplication, Division, % Remainder/Modulo operation ++ Unary increment (var++ same as var=var+1) -- Unary decrement ^ or ** Exponentaion +=, -=, *=, /=, Assignment operator preceded by arithmetic operation (var+=5 %= same as var=var+5) No operator String concatenation (newstr=“new” $3) ?: Ternary operator (expr1 ? expr2 : expr3)
  • 9. Relational Operators. 9 Operator Explanation == Equality operator != Not equal to < Less than <= Less than or equal to > Greater than >= Greater than equal to ~ Contains regular expression !~ Doesn‟t contain regular expression
  • 10. awk patterns 10  Can match either particular lines or ranges of lines  Regular expression patterns  Relational expression patterns  BEGIN and END patterns
  • 11. Regular Expressions 11 Meta character Meaning . Matches any single character except newline * Matches zero or more of the character preceding it e.g.: bugs*, table.* ^ Denotes the beginning of the line. ^A denotes lines starting with A $ Denotes the end of the line. :$ denotes lines ending with : Escape character (., *, [, , etc) [] matches one or more characters within the brackets. e.g. [aeiou], [a-z], [a-zA-Z], [0-9], [[:alpha:]], [a-z?,!] [^] matches any characters others than the ones inside brackets. eg. ^[^13579] denotes all lines not starting with odd numbers, [^02468]$ denotes all lines not ending with even numbers <, > Matches characters at the beginning or end of words
  • 12. Extended Regular Expressions 12 Meta character Meaning | alternation. e.g.: ho(use|me), the(y|m), (they|them) + one or more occurrences of previous character. a+ is same as aa*) ? zero or one occurrences of previous character. {n} exactly n repetitions of the previous char or group {n,} n or more repetitions of the previous char or group {n, m} n to m repetitions of previous char or group. For the above four –re-interval option needs to be specified (....) Used for grouping
  • 13. Regular Expressions – Examples 13 Example Meaning .{10,} 10 or more characters. Curly braces have to escaped [0-9]{3}-[0-9]{2}-[0-9]{4} Social Security number ([2-9][0-9]{2})[0-9]{3}-[0- Phone number (xxx)yyy-zzzz 9]{4} [0-9]{3}[ ]*[0-9]{3} Postal code in India [0-9]{5}(-[0-9]{4})? US ZIP Code with optional four-digit extension
  • 14. Regular Expression Patterns. 14 Example Explanation awk „/pat1/‟ infile Same as grep „pat1‟ infile awk „/pat1/, /pat2/‟ infile Print all lines between pat1 and pat2 repetitively awk „/pat1|pat2/‟ infile Print lines that have either pat1 or pat2 awk „/pat1.*pat2/‟ infile Print lines that have pat1 followed by pat2 with something or nothing in between
  • 15. Relational Expression Patterns. 15 Example Explanation awk „$1==“USA”‟ infile Print the line if the first field is USA awk „$2 !=“xyz”‟ infile Print all lines whose second field is not “xyz” awk „$2 < $3‟ infile Print all lines whose third field is greater than the second awk „$5 ~ /USA/‟ infile Print if the fifth field contains USA awk „$5 !~ /USA/‟ infile Print if the fifth field doesn‟t contain USA awk „NF == 5‟ infile Print lines that have five fields awk „NR == 5, NR==10‟ Print lines 5 to 10 infile awk „NR%5==0‟ infile Print every fifth line (% is the modulo operator) awk „NR%5‟ infile Print everything other than every fifth line awk „$NF ~ /pat1/‟ infile Print if the last field contains pat1
  • 16. awk compound-patterns 16  Compound patterns formed with Boolean operations (&&, ||, !), and range patterns  pat1 && pat2 (compound AND)  pat1 || pat2 (compound OR)  !pat1 (Negation)  pat1, pat2 (range pattern)
  • 17. Compound Pattern Examples 17 Example Explanation awk „/pat1/ && $1==“str1”‟ infile Print lines that have pat1 and whose first field equals str1 awk „/pat1/ || $2 >= 10‟ infile Print lines that have pat1 OR whose second field is greater than or equal to 10 awk „!/pat1/‟ infile Same as grep –v “pat1” infile awk „NF >=3 && NF <=6‟ infile Print lines that have between 3 and six fields awk „/pat1/ || /pat2/‟ infile Same as awk „/pat1|pat2/‟ infile awk „/pat1/, /pat2/‟ infile Print all lines between pat1 and pat2 repetitively awk „!/pat1|pat2/‟ infile Print lines that have neither pat1 nor pat2 awk „NR > 30 && $1 ~ /pat1|pat2/‟ Print lines beyond 30 that have first field infile containing either pat1 or pat2
  • 18. Compound Pattern Examples 18 Example Explanation awk „/pat1/&&/pat2/‟ infile Print lines that have both pat1 and pat2. awk „/pat1.*pat2/‟ infile How is this different from the one above? awk „NR<10 || NR>20‟ infile Print all lines except lines 10 to 20 awk „!(NR >=10 && NR<=20)‟ infile Print lines between 10 and 20. Same as awk ‘NR==10, NR==20’ infile
  • 19. BEGIN and END patterns 19  BEGIN allows actions before any lines are processed.  END allows actions after all lines have been processed  Either or both optional BEGIN {action} [Pattern] {action} END {action}
  • 20. BEGIN 20  Use BEGIN to:  Set initial values for variables  Print headings  Set internal field separator (same as –F on command line) awk „BEGIN {FS=“:”; print “File name”, FILENAME}‟ file2 file2
  • 21. END 21  Use END to:  Perform any final calculations  Print report footers.  Do any thing that must be done after all lines have been processed. awk „END {print NR}‟ file2 file2
  • 22. Creating Actions 22  Actions consist of one or more statements separated by semicolon, newline, or a right-brace.  Types of statements:  Assignment statement (e.g.var1=1)  Flow-control statements  Print control statement
  • 23. Flow-control statements 23 Statement Explanation if (conditional) Perform statement_list1 if conditional is true. {statement_list1} Otherwise statement_list2 if specified [else {statement_listt2}] while (conditional) Perform statement_list while conditional is true {statement_list} for Perform int_expr firt. While conditional_expr is (int_expr;conditional_expr true, perform statement_list and execute ctrl_expr. ;ctrl_expr) {statement_list} break Break from the containing loop and continue with the next statement continue Go to the next iteration of the containing loop without executing the remaining statements in loop next Skip remaining patterns on this line exit Skip the rest of the input and go to the END pattern if one exists or exit.
  • 24. Print-control statements 24 Statement Explanation print [expression_list] Print the expression on stdout unless redirected to [>filename] filename. printf format [, Prints the output as specified in format (like printf expression_list] in C). Has a rich set of format specifiers. [>filename]
  • 25. Variables 25  Provide power and flexibility  Formed with letters, numbers and underscore character.  Can be of either string or numeric type  No need to declare or initialize.  Type implied by the assignment. No $ in front of variables. (e.g. var1=10; job_type=„clerk‟)  Field names ($1, $2, ..$n) are special form of variables. Can be used like any other variable.
  • 26. Arrays 26  One-dimensional arrays: array_name[index]  Index can be either numeric or string. Starts with 1 if numeric  No special declaration needed. Simply assign values to an array element.  No set size. Limited only by the amount of memory on the machine.  phone[“home”], phone[“mobile”], phone[var1], phone[$1], ranks[1]
  • 27. Multi-Dimensional arrays 27  Arrays are one-dimensional.  Array_name[1,2] not supported  Concatenate the subscripts to form a string which could be used as the index: array_name[1”,”2] Space is the concatenation operator. “1,2”, a three character string is the index.  Use SUBSEP, subscript separator, variable to eliminate the need to have double quotes around the comma.
  • 28. Built-in functions 28 Function Explanation cos(awk_expr) Cosine of awk_expr exp(awk_expr) Returns the exponential of awk_expr (as in e raised to the power of awk_expr) index(str1, str2) Returns the position of strt2 in str1. length(str) Returns the length of str log(awk_expr) Base-e log of awk_expr sin(awk_expr) Sine of awk_expr sprintf(frmt, awk_expr) Returns the value of awk_expr formatted as per frmt sqrt(awk_expr) Square root of awk_expr split(str, array, [field_sep]) Splits a string into its elements and stores into an array substr(str, start, length) Returns a substring of str starting at position “start” for “length” characters. toupper(), tolower() Useful when doing case-insensitive searches
  • 29. Built-in functions contd. 29 Function Explanation sub(pat1, “pat2”, [string]) Substitute the first occurrence of pat1 with pat2 in string. String by default is the entire line gsub(pat1, “pat2”, [string]) Same as above, but replace all occurrences of pat1 with pat2. match(string, pat1) Finds the regular expression pat1, and sets two special variables (RSTART, RLENGTH) that indicate where the regular expression begins and ends systime() returns the current time of day as the number of seconds since Midnight, January 1, 1970
  • 30. Case Insensitive Match 30  Case insensitive match:  awk „BEGIN {ignorecase=1} /PAT1/‟  awk „tolower($0) ~ /pat1/ …‟
  • 31. User-Defined functions 31 Gawk allows user defined functions #!/usr/bin/gawk -f { if (NF != 4) { error("Expected 4 fields"); } else { print; } } function error ( message ) { if (FILENAME != "-") { printf("%s: ", FILENAME) > "/dev/tty"; } printf("line # %d, %s, line: %sn", NR, message, $0) >> "/dev/tty"; }
  • 32. Very Simple Examples 32  Find the average filesize in a directory  Find the users without password  Convert String to Word (string2word.awk)  List the file count and size for each user (cnt_and_size.awk)
  • 33. Awk one-liners 33 Example Explanation awk‟{print $NF}‟ infile Print the last field in each line awk‟{print $(NF-1)}‟ infile Print the field before the last field. What would happen if () are removed? What happens if there is only one field awk‟NF‟ infile Print only non-blank lines. Same as awk „/./‟ awk „{print length, $0)‟ infile Print each line preceded by its length. awk „BEGIN {while Print 1 to 10 (++x<11) print x}‟ awk „BEGIN {for (i=10; Print 10 to 50 in increments of 4 i<=50; i+=4) print i}‟ awk „{print; print “”}‟ infile Add a blank line after every line awk „{print; if (NF>0) print Add a blank line after every non-blank line “”}‟ infile
  • 34. Awk one-liners 34 Example Explanation awk‟NF !=0 {++cnt} END Count the number of non-blank lines {print cnt}‟ infile ls –l | awk „NR>1 {s+=$5} Return the average file size in a directory END {print “Average:” s/(NR-1)}‟ awk „/pat1/?/pat2/:/pat3/‟ uses ternary operator ?: Equivalent to awk „/pat1/ && /pat2/ || pat3‟ except for lines containing both pat1 and pat3 infile awk „NF<10?/pat1/:/pat2/‟ Use pat1 if number of fields is less than 10; infile otherwise use pat2 awk „ORS=NR%3?” ”:”n”‟ Join three adjacent lines. ORS is the output record infile separator awk „ORS=NR%3?”t”:”n” Print the first field three to row. ORS is the output {print $1}‟ infile record separator awk „FNR < 11‟ f1, f2, f3 Concatenate the first 10 lines of f1, f2, and f3.
  • 35. Awk one-liners 35 Example Explanation awk „length < 81‟ Print lines that are shorter than 81 characters awk „/pat1/, 0‟ Print all lines between the line containing pat1 and end of file awk „NR==10, 0‟ Print lines 10 to the end of file. The end condition “0” represents “false”. awk '{ sub(/^[ t]+/, ""); Trim the leading tabs or spaces. Called ltrim print }' awk '{ sub(/[ t]+$/, ""); Trim the trailing tabs or spaces. Called rtrim print }' awk '{ gsub(/^[ t]+|[ Trim the white spaces on both sides t]+$/, ""); print }'
  • 36. Awk one-liners 36 Example Explanation awk '/pat1/ { gsub(/pat2/, Replace pat2 with “str” on lines containing pat1 “str") }; { print }' awk '{ $NF = ""; print }' Delete the last field on each line
  • 37. Awk one-liners 37  http://www.catonmat.net/blog/awk-one-liners- explained-part-one/
  • 38. Translators 38  awk2c – Translates awk programs to C  awk2p – Translates awk progrms to Perl
  • 39. References 39  sed & awk by Dale Dougherty & Arnold Robins  http://www.grymoire.com/Unix/Awk.html  http://www.vectorsite.net/tsawk.html  http://snap.nlc.dcccd.edu/reference/awkref/gawk _toc.html
  • 40. Q&A 40  devel-awk@yahoo-inc.com
  • 41. Unanswered questions 41  How to print lines that are outside a block of lines? (print lines that are not enclosed by /pat1/,/pat2/  Does awk support grouping and back-referencing (e.g. identify adjacent duplicate words)?