Dies ist eine alte Version des Dokuments!
Inhaltsverzeichnis
Shell commands
awk
How To Use awk In Bash Scripting
by nixCraft on August 15, 2009 · 29 comments· LAST UPDATED August 14, 2009 see: http://www.cyberciti.biz/faq/bash-scripting-using-awk/
in BASH Shell, CentOS, Debian / Ubuntu
How do I use awk pattern scanning and processing language under bash scripts? Can you provide a few examples?
Awk is an excellent tool for building UNIX/Linux shell scripts. AWK is a programming language that is designed for processing text-based data, either in files or data streams, or using shell pipes. In other words you can combine awk with shell scripts or directly use at a shell prompt.
Print a Text File
awk '{ print }' /etc/passwd
OR
awk '{ print $0 }' /etc/passwd
Print Specific Field
Use: as the input field separator and print first field only i.e. usernames (will print the the first field. all other fields are ignored):
awk -F':' '{ print $1 }' /etc/passwd
Send output to sort command using a shell pipe:
awk -F':' '{ print $1 }' /etc/passwd | sort
Pattern Matching
You can only print line of the file if pattern matched. For e.g. display all lines from Apache log file if HTTP error code is 500 (9th field logs status error code for each http request):
awk '$9 == 500 { print $0}' /var/log/httpd/access.log
The part outside the curly braces is called the „pattern“, and the part inside is the „action“. The comparison operators include the ones from C:
== != < > <= >= ?:
If no pattern is given, then the action applies to all lines. If no action is given, then the entire line is printed. If „print“ is used all by itself, the entire line is printed. Thus, the following are equivalent:
awk '$9 == 500 ' /var/log/httpd/access.log awk '$9 == 500 {print} ' /var/log/httpd/access.log awk '$9 == 500 {print $0} ' /var/log/httpd/access.log