SlideServe
Menu
Préférences
© SlideServe · v2026

Introduction to AWK: A Versatile Scripting Language for Text Processing

daisy avatar
18 diapositives · 346 Views · Aug 21, 2014

AWK is a powerful programming language designed for text and string manipulation within shell scripts. Ideal for processing input data in records and fields, AWK enables users to perform arithmetic and string operations, manage loops and conditionals, produce formatted reports, and execute Unix commands directly from scripts. With simple syntax and options for advanced usage, it allows for efficient processing and management of data in files. This introduction covers basic commands, examples, and built-in variables that make AWK a valuable tool for programmers and system administrators.

Introduction to AWK: A Versatile Scripting Language for Text Processing - slide 1
01 / 18Introduction to AWK: A Versatile Scripting Language for Text Processing
1 / 17

Télécharger la présentation

Introduction to AWK: A Versatile Scripting Language for Text Processing
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

  2. What is "awk"? • awk = programming language • text/string manipulation within shell script • useful for input data in records/fields

  3. Typical uses • Perform arithmetic and string operations • Use loops and conditionals • Produce formatted report • Process UNIX command arguments • Execute UNIX commands from script • …

  4. Examples (1) • How to print the first three fields of each record on separate lines • Can we do by shell script and "cut" ? • We can do by "awk" in one command awk -F: '{ print $1; print $2; print $3 }' /etc/passwd

  5. Syntax • Command forms awk [options] 'script' var=value file(s) awk [options] -f scriptfile var=value file(s) • Variable assignment on command-line • "value" can be • string, numeric constant • shell variable ($name) • command substitution (`cmd`)

  6. Options • Standard options • Advanced options (see man pages)

  7. Patterns and procedures • Script is pattern { action } • Both are optional • pattern missing, apply to all lines • action missing, matched line is printed

  8. Patterns

  9. Patterns

  10. Procedures • Separated by newline, semicolons • Contained in "{}" • Command groups

  11. Example (2) • Print first field of each line { print $1 } • Print all lines containing pattern /pattern/ • Print first field of lines containing pattern /pattern/ { print $1 } • Select record having > 2 fields NF > 2

  12. Example (3) • Print only lines which first field matches URGENT $1 ~ /URGENT/ { print $3, $2 } • Print number of lines matching pattern /pattern/ {x++} END {print x} • Sum up column 2 and print the total {total += $2} END { print total}

  13. Example (4) • Print lines containing < 20 characters length($0) < 20 • Print lines of 7 fields and beginning with "Name:" NF==7 && /^Name:/

  14. Example (5) • Print fields in reverse order one per line { for (i=NF; i>=1; i--) print $i; }

  15. Built-in variables

  16. Operators

  17. awk functions and commands • Control flow • break, continue, do/while, exit, for, if/else, return, while • Arithmetic • rand, sin, cos,… • String • IO • print, printf, getline,…

  18. Exercises is NEXT