Linux operating system-awk basic usage

Source: Internet
Author: User
Tags ldap gopher


Gawk (GNU awk)
The GNU version of awk in Unix, which completes the work of grep and SED
Supports mathematical operations, processes that control a large number of variables and functions built into it


How awk commands work:
As with SED, it is a line of reading, processing
SED acts on a whole line of processing, and awk divides a line into several fields to handle

Data field variables for awk:
$ A indicates the entire line of text
$ = The first data field in a text
$ = A second data field in text
$n represents the nth data field in a text


Awk uses-F to specify delimiters:
The default field delimiter is any white space character (space or tab)
Compare the difference between cut and awk for example

The execution of AWK commands:
Executes a statement in a begin{commands} statement block
Reads the 1th line from a file or stdin,
There is no pattern match, if none executes the statement in {},
If yes, check if the whole line matches the pattern, and if it matches, execute the statement in {}.
If it does not match, the statement in {} is not executed, then the next line is read
Repeat this process until all rows have been read
Executes a statement in a end{commands} statement block


Basic syntax for the awk command:
Awk-f delimiter '/mode/{action} ' input file
Awk's instructions must be enclosed in single quotes
The action of awk must be enclosed in curly braces.
A pattern can be a regular expression, a conditional expression, or a combination of two
If the pattern is a regular expression to use/delimiter
To separate a number of actions from one another;

Examples of awk basic commands:
awk '/bash/'/etc/passwd
Only mode has no action result and grep, display
W.H.O. | awk ' {print $} '
Actions are performed directly without a pattern
Awk-f: '/^h/{print $1,$7} '/etc/passwd
Print execution display function to output text to stdout
Displays the first and seventh columns of a line starting with H with a colon delimiter
Awk-f: '/^[^h]/{print $1,$7} '/etc/passwd
Do not display the first and seventh columns of rows beginning with H
Awk-f ' [:/] ' {print $1,$10} '/etc/passwd
Show 1th and 10th columns as separators

The awk command operator:
Regular expressions are consistent with bash
Mathematical operations: +,-,*,/,%,++,--
Logical relationship: &&, | |,!
Comparison operator: >,<,>=,!=,<=,== ~!~
Text data expression: = = (exact match)
~ Tilde Indicates the pattern following the match
W.H.O. | awk ' $ $ ~/pts/{print '
Awk-f: ' $ $ ~/\<...\>/{print $1,$3} '/etc/passwd
SEQ 100 | awk ' $5 = = 0 | | $ ~/^1/{print} '
Awk-f: ' $ = = ' Root ' {print $1,$3} '/etc/passwd

Examples of awk basic commands:
Awk-f: ' $3>=500{print $ '/etc/passwd
Display user names with UID greater than or equal to 500 rows
Awk-f: ' $3>=500 && $3<=60000{print '/etc/passwd
Show Normal User name
Awk-f: ' $ $! = $4 {print \/etc/passwd} '
User name showing UID not equal to GID
Awk-f: '/^h/&&/bash/{print '/etc/passwd
Displays the user name starting with H and containing the hash field of the normal user
PS aux | awk ' $ <=10 {print $11} '
awk ' begin{print ' line one\nline two\nline three "} '
Show back three rows
awk ' end{print ' line one\nline two\nline three "} '
Press Ctrl+d to start displaying the following three lines
awk ' begin{print ' Start ... "}{print $1}end{print" END ... "} ' test
Display the contents of the file and precede it with start and trailing end
awk ' Begin{i=0}{i++}end{print i} '/etc/passwd
Show the number of lines in a file
awk ' {print NF} '/etc/grub.conf
Show the number of fields per row
awk ' {print $, $NF} '/etc/grub.conf
Show the first and last fields of each row
awk ' {print nr,$0} '/etc/grub.conf
Display the contents and line numbers of each row
Awk-f: ' begin{ofs= '---"}{print $1,$7} '/etc/passwd
Displays the first and seventh columns, separated by---
awk ' begin{fs= ': "}/bash$/{print nr,$1}end{print NR} '/etc/passwd
Displays the user name that matches the pattern and the line number at the end of which the row number is displayed

Examples of awk basic commands:
awk ' nr==3,nr=5 '/etc/grub.conf
Show 3 to 5 lines of files
awk ' nr<=10 '/etc/fstab
Show the first 10 lines of a file

Analyze the difference between the following three commands, why
awk ' Begin{print NR} '/etc/passwd 0
awk ' {print NR} '/etc/passwd Total rows
awk ' End{print NR} '/etc/passwd total number of rows
Analyze the execution result of the following command
Awk-f: ' {print $NR} '/etc/passwd
[Email protected] lianxi]# awk-f: ' {print $NR} ' passwd
Root
X
2
4
Lp
/sbin
/sbin/shutdown
[Email protected] lianxi]#
Awk-f: ' {print NR, NF, $ $, $NF, $ (NF-1)} '/etc/passwd


[Email protected] lianxi]# awk-f: ' {print NR, NF, $ $, $NF, $ (NF-1)} '/etc/passwd
1 7 root/bin/bash/root
2 7 Bin/sbin/nologin/bin
3 7 Daemon/sbin/nologin/sbin
4 7 Adm/sbin/nologin/var/adm
5 7 LP/SBIN/NOLOGIN/VAR/SPOOL/LPD
6 7 Sync/bin/sync/sbin
7 7 Shutdown/sbin/shutdown/sbin
8 7 Halt/sbin/halt/sbin
9 7 Mail/sbin/nologin/var/spool/mail
7 UUCP/SBIN/NOLOGIN/VAR/SPOOL/UUCP
7 Operator/sbin/nologin/root
7 Games/sbin/nologin/usr/games
7 Gopher/sbin/nologin/var/gopher
7 ftp/sbin/nologin/var/ftp
7 Nobody/sbin/nologin/
7 Dbus/sbin/nologin/
7 Usbmuxd/sbin/nologin/
7 Rpc/sbin/nologin/var/cache/rpcbind
7 Vcsa/sbin/nologin/dev
7 Rtkit/sbin/nologin/proc
7 ABRT/SBIN/NOLOGIN/ETC/ABRT
7 AVAHI-AUTOIPD/SBIN/NOLOGIN/VAR/LIB/AVAHI-AUTOIPD
7 Saslauth/sbin/nologin/var/empty/saslauth
7 Postfix/sbin/nologin/var/spool/postfix
7 Haldaemon/sbin/nologin/
7 GDM/SBIN/NOLOGIN/VAR/LIB/GDM
7 NTP/SBIN/NOLOGIN/ETC/NTP
7 apache/sbin/nologin/var/www
7 Mysql/bin/bash/var/lib/mysql
7 Rpcuser/sbin/nologin/var/lib/nfs
7 Nfsnobody/sbin/nologin/var/lib/nfs
7 Pulse/sbin/nologin/var/run/pulse
7 sshd/sbin/nologin/var/empty/sshd
7 Tcpdump/sbin/nologin/
7 Squid/sbin/nologin/var/spool/squid
7 named/sbin/nologin/var/named
Panax 7 Tss/sbin/nologin/dev/null
7 Ldap/sbin/nologin/var/lib/ldap
7 WL/BIN/BASH/HOME/WL

First-column file system showing only df-h results
Displays line numbers and user names for lines 5th and 10th of the passwd file
Use NF variable to display the contents of the penultimate column of the passwd file
Awk-f: ' {print $ (NF-1)} ' passwd
Displays the user name for lines 5th through 10th in the passwd file
Awk-f: ' nr==1,nr==10 {print '} ' passwd
Display the user name of the 7th column in the passwd file that is not bash
Awk-f: '/nologin$/{print $ ' passwd
The line number that displays the passwd file is 5 and the line number ends
Awk-f: ' Nr==5{print nr,$0} ' passwd
Only display IP with ifconfig (cannot use TR or cut command)
Ifconfig eth0 |awk-f: ' Nr==2{print $ ' |awk-f ' ' {print '} '
Use awk to display inbound and outbound traffic for eth0 (bytes)
Ifconfig eth0 |awk-f "" ' Nr==8{print $3,$4,$7,$8} '
Use the awk command to count the number of users starting with R, showing the following effect
Find Results
Root
Rpc
Rtkit
Rpcuser
4

The awk command references a shell variable:
-V Introduces Shell variables
#!/bin/bash
Name=haha
echo | awk ' {print $name} '
#!/bin/bash
Name=haha
echo |awk-v abc= $name ' {print ABC, $name} '


There are many functions built into the AWK programming language:
For example, a function that calculates the number of characters by length checks if a user has a null password
Awk-f: ' Length ($) ==0{print $ '/etc/passwd/etc/shadow
Display lines that are longer than 60 characters in a file
Wk ' Length ($) >60 {print nr,$0} '/etc/passwd


If statement
Single Branch
if (condition) statement1
Awk-f: ' {if ($ ~/\<...\>/) print $} '/etc/passwd
Awk-f: ' {if ($ >=) print $1,$7} '/etc/passwd
Dual Branch
if (condition) statement1; else statement2
Awk-f: ' {if ($! = 0) ' Print $ ', else print $} '/etc/passwd
Multi-Branch
Awk-f: ' {if ($1== ' root ') print $, else if ($1== "FTP") print $, else if ($1== "Mail") print $; else print NR} '/etc/passwd

Use Awk's system command to create a directory with the same name as the user name in the/TMP/etc/passwd:
Awk-f: ' {System ("mkdir/tmp/")} '/etc/passwd
Awk-f "" ' {if ($7) >60) {print nr,$3,$4,$5,$6,substr ($7 1,55)} else {print nr,$3,$4,$5,$6,$7}} ' wl.txt fetch top 55


awk sums the columns:
Statistics total size of files ending with. conf in/etc directory
find/etc/-type f-name "*.conf" |xargs ls-l | awk ' {sum+=$5} end{print sum} '
If you want to match the first column to accumulate, you need an array of awk and a For loop
Cat Xferlog | Cut-d '-f7,8 | Sort-n >/tmp/file1
awk ' {a[$1]+=$2}end{for (i in a) print I "" a[i]/1024/1024} '/tmp/file1 | Sort-rn-k2
awk for Row summation
Echo 1 2 3 4 5 | awk ' {for (i=1;i<=nf;i++) sum+= $i; Print sum} '
Seq-s ' 100 | awk ' {for (i=1;i<=nf;i++) sum+= $i; Print sum} '


This article is from the "one Gourd soy sauce" blog, please be sure to keep this source http://zhushenghang.blog.51cto.com/11105203/1826756

Linux operating system-awk basic usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.