Disclaimer: This article is a blogger hanchao5272 original articles, please indicate the source and leave the original link address, thank you! https://blog.csdn.net/hanchao5272/article/details/89205901
Series catalog References Portal: shell introductory study notes - Prologue
awk IO operation
Statement | description |
---|---|
getline | Get the next row as input values of $ 0 |
There getline | Get the next row as an input value of var |
command getline [var] | Run command is output to the conduit or var $ 0 |
next | Stop processing the current input record |
system(cmd-line) | Execute command and return status |
Print this record | |
print … >> file | Append output to a file |
print … | command | Print output as command input |
printf fmt, expr-list [>file] | Written and formatted output files |
The next line of input to get $ 0 (getline)
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '/3/{print}'
3
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '/3/{getline;print}'
4
Get the next line is input to var (getline var)
# 简单实用
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '/3/{getline a;print a}'
4
# 合并文件列
[worker@c2-a02-126-10-4 hanchao]$ cat a.txt
a
b
c
[worker@c2-a02-126-10-4 hanchao]$ cat b.txt
1
2
3
[worker@c2-a02-126-10-4 hanchao]$ awk '{print $0}' b.txt
1
2
3
[worker@c2-a02-126-10-4 hanchao]$ awk '{line < "a.txt";print $0,line}' b.txt
1
2
3
[worker@c2-a02-126-10-4 hanchao]$ awk '{getline line < "a.txt";print $0,line}' b.txt
1 a
2 b
3 c
Run command is output to the pipeline or $ 0 var (command getline [var])
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{"echo hhhh"|getline line;print line"--"}'
hhhh--
Skip deal with the Bank next
# 处理当前行
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{if(NR==3){print "=="$0"==";}else{print}}'
1
2
==3==
4
5
# 跳过当前行,处理下一行
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{if(NR==3){getline;print "=="$0"==";}else{print}}'
1
2
==4==
5
# 跳过当前行
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{if(NR==3){next;print "=="$0"==";}else{print}}'
1
2
4
5
System result of the command system ()
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{if(system("echo 1")==0) print "yes";else print "no"}'
1
yes
Append output to a file print ... >> file
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{print > "file.txt"}'
[worker@c2-a02-126-10-4 hanchao]$ cat file.txt
1
2
3
4
5
Print output as piped input print ... | commond
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{print |"grep 3"}'
3
Formatted output printf fmt, expr-list [> file]
Format | description |
---|---|
\n |
Wrap |
\t,\v |
Horizontal tab, vertical tab |
\b |
Backspace |
\r |
Carriage return |
\f |
Page breaks |
%c |
A single character |
%s |
A string |
%.ns |
Output string, n being output several characters |
%d,%i |
An integer |
%e,%E |
Scientific notation |
%f,%g,%G |
A floating-point number |
%m.nf |
Floating-point output, m is an integer of output bits, n being the number of decimal places output |
%o |
Octal |
%u |
Decimal |
%x,%X |
Hexadecimal, using a / A to f / F represents 10 to 15 |
%% |
Output a single% |
%-5s |
Left, left aligned to the parameters of each field, a width of 5 |
%-4.2f |
Left, a width of 4, with two decimal places |
%5s |
Right justified, right justified without dash indicates |
Newline \ n
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "Hello\nworld\n"}'
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{print "Hello\nworld\n"}'
Hello
world
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "Hello\nworld\n"}'
Hello
world
[worker@c2-a02-126-10-4 hanchao]$
Horizontal tab \ t, vertical tab \ v
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "ID\tName\tAddress\n111\tJone\t18\n2\tSmith\t33\n"}'
ID Name Address
111 Jone 18
2 Smith 33
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "ID\vJone\v33\n"}'
ID
Jone
33
Backspace \ b
# 退格:最后一个字符被删除
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "12345\b 12345\n"}'
1234 12345
Carriage return \ r
# 回车:回车之后的内容覆盖回车之前的内容
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "54321\r12345\n"}'
12345
Formfeed \ f
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "ID\fJone\f33\n"}'
ID
Jone
33
% C character
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%c,%c\n",65,"hello"}'
A,h
- Value will be converted ASCII code corresponding to characters.
- The first character string interception.
% S,%. Ns string
## %s
# 一个字符串
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%s\n","hello"}'
hello
# 将换行符替换成逗号
[worker@c2-a02-126-10-4 hanchao]$ seq 5 |awk '{if(NR!=5){printf "%s,",$0}else{print}}'
1,2,3,4,5
# 输出一个字符
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%.1s\n","hello"}'
h
% D,% i integer
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%d %i\n",11.22,33.44}'
11 33
% E,% E scientific notation
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%e %E\n",11.22,33.44}'
1.122000e+01 3.344000E+01
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%.1e %.3E\n",11.22,33.44}'
1.1e+01 3.344E+01
% F,% g,% G,% m.nf float
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%f %.2f\n",11.22,33.44}'
11.220000 33.44
# %g和%G会自动删除对数值无意义的0
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%f %g %G\n",11.22,33.44,44.5500}'
11.220000 33.44 44.55
# %m.nf: m表示整数长度,n表示小数点长度
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "%f %1.2f %3.1f\n",11.22,33.44,44.5500}'
11.220000 33.44 44.5
% O octal,% u decimal,% x hexadecimal
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "十进制=%u,八进制=%o,十六进制=%X\n",1001,1001,1001}'
十进制=1001,八进制=1751,十六进制=3E9
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "十进制=%u,八进制=%o,十六进制=%x\n",1001,1001,1001}'
十进制=1001,八进制=1751,十六进制=3e9
Output %% percent sign
[worker@c2-a02-126-10-4 hanchao]$ awk 'BEGIN{printf "33.19%%\n" }'
33.19%
% -Ns,% ns,% m.ns alignment
%ns
: Right alignment, a width of n%-ns
: Left, width n%m.ns
: Right alignment, a width of the integer part of m, a width of the fractional part of n
[worker@c2-a02-126-10-4 hanchao]$ printf "T001 David 98.912\nT02 Jone 100\n" |awk 'BEGIN{printf "%-5s\t%-8s\t%-3s\n","ID","Name","Score"}{printf "%-5s\t%-8s\t%-3.2f\n",$1,$2,$3}'
ID Name Score
T001 David 98.91
T02 Jone 100.00