|
First, awk Profile
1,
awk is the first three letters of the last name, on behalf of the three authors of the language, there are many versions of awk, including: Legacy awk, the new version of awk (nawk), GNU awk (gawk) and the like.
There awk command awk program, enclosed in quotes or written instructions in the file and the input file these parts.
2,
[Root @ rhel helinbash] # which awk
/ Bin / awk
[Root @ rhel helinbash] # which gawk
/ Bin / gawk
[Root @ rhel helinbash] # ls -l / bin / awk / bin / gawk
lrwxrwxrwx 1 root root 4 Oct 10 2013 / bin / awk -> gawk
-rwxr-xr-x 1 root root 320416 Jan 15 2007 / bin / gawk
[Root @ rhel helinbash] #
NOTE: After the examples are based on gawk command
Second, awk works
1,
The following names follow the steps to resolve the file name, for example awk processing
(1)
vim names
Tom Savage 100
Molly Lee 200
John Doe 300
(2)
[Root @ rhel helinbash] # cat names.txt | cut -d '' -f 2
Savage
Lee
[Root @ rhel helinbash] # cat names.txt | cut -d '\ t' -f 2
cut: the delimiter must be a single character
Try `cut --help 'for more information.
[Root @ rhel helinbash] #
(3)
[Root @ rhel helinbash] # gawk '{print $ 1, $ 3}' names.txt
Tom 100
Molly 200
John 300
[Root @ rhel helinbash] #
[Root @ rhel helinbash] # gawk '{print $ 1 $ 3}' names.txt
Tom100
Molly200
John300
[Root @ rhel helinbash] #
2,
Schematic
FS: Field separator (separator)
OFS: Output Field Separator
Third, from the input file
1,
format:
gawk '/ match string /'
gawk '{} processing operation'
gawk '/ match string / processing operation {}' file name
2,
[Root @ rhel helinbash] # gawk '/ root /' / etc / passwd
root: x: 0: 0: root: / root: / bin / bash
operator: x: 11: 0: operator: / root: / sbin / nologin
[Root @ rhel helinbash] # grep root / etc / passwd
root: x: 0: 0: root: / root: / bin / bash
operator: x: 11: 0: operator: / root: / sbin / nologin
[Root @ rhel helinbash] #
3,
[Root @ rhel helinbash] # gawk '/ ^ root /' / etc / passwd
root: x: 0: 0: root: / root: / bin / bash
[Root @ rhel helinbash] #
4,
[Root @ rhel helinbash] # gawk '/ ^ root /'
root
root
root
root
studnet
t ^ H ^ [[3 ~ ^ H ^ H ^ H this is a demo string wih ^ H ^ H iclcude root key woard
root hello abc
root hello abc
[Root @ rhel helinbash] #
Note: The red font is filtered output, this is the interactive Run gawk
5,
[Root @ rhel helinbash] # gawk -F: '{print $ 1 $ 3}' / etc / passwd
root0
bin1
daemon2
adm3
lp4
sync5
shutdown6
halt7
mail8
news9
uucp10
operator11
games12
gopher13
ftp14
nobody99
nscd28
vcsa69
rpc32
mailnull47
smmsp51
pcap77
ntp38
dbus81
avahi70
sshd74
rpcuser29
nfsnobody65534
haldaemon68
avahi-autoipd100
xfs43
gdm42
sabayon86
Oracle500
named25
[Root @ rhel helinbash] #
6,
[Root @ rhel helinbash] # gawk -F: '/ root / {print $ 1 $ 3}' / etc / passwd
root0
operator11
[Root @ rhel helinbash] #
7,
Print formatted output function
awk command operation processing section is on the "{}" (in parentheses) in; print function variables and characters mixed with output. As the echo command linux
Fourth, from the command input
1,
Awk can also handle the results received through the pipeline linux commands, shell typically uses awk do deep processing.
(1)
format:
Command | gawk '/ match string /'
Command | gawk '{} processing operation'
Command | gawk '/ match string / processing operation {}'
(2)
[Root @ rhel helinbash] # date
Mon May 26 10:10:01 CST 2014
[Root @ rhel helinbash] # date | gawk '{print "Month:" $ 2 "\ nYear:", $ 6}'
Month: May
Year: 2014
[Root @ rhel helinbash] #
(3) Note that the above example, a direct connection behind Month $ 2, and the other is between $ 6 and the Year of the comma, the decision by the OFS. |
|
|
|