awk note

gkernel 2006-07-26 05:52:16
awk '/pattern/ {program}'

...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunyata 2006-07-28
  • 打赏
  • 举报
回复
NR: number of records
NF: number of fields
FILENAME: current input file name
FS: field separator
RS: record separator
OFS: output field separator
ORS: output record separator
OFMT: format for numeric output

change input: $n="" # n is a number
array: arr[1], arr["one"]

exponentiation: ^

functions:
length, length(<string>)
substr(<string>, <start>, <len>)
split(<string>, <array>, [<separator>])
index(<string>, <searchstring>) # 1 based

for (<var> in <array>) {
}

next: get another line of input

print, printf, sprintf # can use redirection operator: print $1 > file
gkernel 2006-07-28
  • 打赏
  • 举报
回复
修正错误:~不是==,~和//连用,==和""连用
gkernel 2006-07-27
  • 打赏
  • 举报
回复
awk -F <delimiter>
awk <vars> # e.g. awk a=1 x=123

search pattern:
$1 !~ /abc/ # ~ is ==
$1 != "abc"
/start/,/end/
NR==1,NR==10
NF=0
(NR >= 30) && ($1 == "china") # && and ||
(($1+0) == $1) # ture if $1 is a number (a trick)

number (only in base 10):
123
3.14
-2.1e10


gkernel 2006-07-27
  • 打赏
  • 举报
回复
awk 'BEGIN {<initializations>}
<search pattern 1> {<program actions>}
<search pattern 2> {<program actions>}
...
END {<final actions>}'

print $1 $2 // concatenate
print $1,$2 // seperate by space

NR: number of records
NF: number of fields

any string is a variable

awk -f program_file_name

printf("<format>", <parameters>); # same as C

comments: #

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧