awk

本文深入探讨了awk程序的主要结构、使用Shell命令的方法、内部使用shell变量、内置变量、数组操作、特殊处理以及正则表达式的应用。重点介绍了awk中的数组、FS与SUBSEP的指定选项、以及如何在实际场景中灵活运用awk进行数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,awk程序的主要结构(awk手册):

awk程序中主要语法是 Pattern { Actions}, 故常见之awk 程序其型态如下 :

Pattern1 { Actions1 }

Pattern2 { Actions2 }

......

Pattern3 { Actions3 }

2,awk 程序中使用 Shell 命令

方法1,awk output 指令 | "Shell 接受的命令"

              echo "3 1 5"|awk'{printf("%d\n%d\n%d\n",$1,$2,$3)|"sort -n"}'

方法2 , "Shell 接受的命令" | awk input 指令

                echo|awk  '{ while("ps"|getlineline ) print line }'

方法3,使用system()

方法4,使用print cmd | “/bin/bash”

3,awk内部使用shell变量(赋值变量要加引号)

方法1   sh_v="shellvariable";echo |awk '{print awk_v}'       awk_v="$sh_v"

方法2   sh_v="shellvariable";echo|awk -v awk_v="$sh_v"      '{print awk_v}'

Gawk executes AWK programs in the following order.  First, all variable assignments specified via the -v option  are performed. Next, gawk compiles the program into an internal form.  Then, gawk executes  the  code  in  the  BEGIN  block(s)  (if  any), and then proceeds toread each file named in the ARGV array.  If there are no files  named on the command line, gawk reads the standard input. If a filename on the command line has the form var=val it is treated as a variable  assignment.   The  variable  var will be assigned the value val.  (This happens after any BEGIN block(s) have been run.)  Command line variable assignment is most useful for dynamically assigning values to the variables AWK uses to control how  input  is  broken into fields and records.  It is also useful for controlling state if multiple passes are needed over   a single data file.

4,AWK内置变量

ARGC        The number of command line arguments 

ARGV        Array  of  command  line arguments.  The array is indexed from 0 to ARGC - 1

NF          The number of fields in the current input record.

FS          The input field separator, a space by default。

SUBSEP      The character used to separate multiple subscripts in array elements, by default "\034".

5,AWK中的数组

awk中的数组是关联数组(associative arrays),因此下标可以是数字也可以是字符串。

awk中的多维数组 下标 是通过SUBSEP内置变量连接起来的。如:

              i = "A"; j = "B"; k = "C"
              x[i, j, k] = "hello, world\n"

这个数组的内部存储为x["A\034B\034C"]="hello, world\n"。即,多维数组的下标为通过SUBSEP变量连接起来,作为一个一维的下标存储。

数组的操作——遍历数组与判断数组元素存在:

The special operator in may be used in an if or for statement to see if an array has an index consisting of a particular value.

              for (var in array) statement
              if (val in array)  print array[val]

数组的操作——删除数组或数组元素:

  1. delete array                     #删除整个数组
  2. delete array[item]           # 删除某个数组元素(item)

6, AWK特殊处理

   space       String Operators concatenation.   awk中的空格有特殊意义  即用空格来连接2个字符串

7,Fields分隔符FS指定选项-F 可以为Regular Expressions

  如用字符串DELIMITER作为分隔符,截取字符串:echo name1-DELIMITER-id1|awk -F'-DELIMITER-' '{print $2}'

 如给-F选项传递多个分隔符:cat file | awk -F'[/=]' '{print $3 "\t" $5 "\t" $8}'

The value isa single-character string or a multicharacter regular expression that matches the separations between fields in an input record.

https://www.gnu.org/software/gawk/manual/html_node/Reading-Files.html#Reading-Files

8,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值