linux - 这段代码有什么作用? (awk)

标签 linux awk

这里我有一部分 awk 代码来解析文件,但输出不是我想要的 100%。

match($0,/root=[^,]*/){
        n=split(substr($0,RSTART+5,RLENGTH-5),N,/:/)

我的问题是我无法 100% 判断这段代码到底在做什么...... 谁能告诉我这两行到底是做什么的?

编辑: 我只是想知道代码做了什么,所以我可以自己修复它,所以请不要问类似这样的问题:你解析的文件是什么样子的? ..

最佳答案

match(s, r [, a])

Returns the position in s where the regular expression r occurs, or 0 if r is not present, and sets the values of RSTART and RLENGTH. Note that the argument order is the same as for the ~ operator: str ~ re. If array a is provided, a is cleared and then elements 1 through n are filled with the portions of s that match the corresponding parenthesized subexpression in r. The 0'th element of a contains the portion of s matched by the entire regular expression r. Subscripts a[n, "start"], and a[n, "length"] provide the starting index in the string and length respectively, of each matching substring.

substr(s, i [, n])

Returns the at most n-character substring of s starting at i. If n is omitted, the rest of s is used.

split(s, a [, r])

Splits the string s into the array a on the regular expression r, and returns the number of fields. If r is omitted, FS is used instead. The array a is cleared first. Splitting behaves identically to field splitting, described above.

因此,当 match 在行 ($0) 中找到匹配 /root=[^,]*/ 的内容时,它将返回该位置(非零整数是awk 的 truth-y) 并且操作将执行。

该操作然后使用 RSTARTRLENGTHmatch 设置以获取匹配行的子字符串(减去 root = 因为 +5/-5) 然后将其拆分到 上的数组 N 中: 并保存拆分为 n 的字段数。

这可能会更改为 match($0,/root=([^,]*)/, N) 作为模式,然后使用 N[1,"start"] 如果需要,可以在操作中代替 substr

关于linux - 这段代码有什么作用? (awk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26631867/

相关文章:

linux - ELF的基址

split - 在 awk 中增加字母变量

unix - 文件的第二列等于一些变量

awk 关联数组增长很快

linux - 您对新的 Debian 发布周期有何看法?

php - WordPress 内部服务器错误

c++ - 什么是 : MultiByteToWideChar & WideCharToMultiByte? 的 Linux 等价物

linux - 如何通过 popen() 将参数传递给我的 c 程序中的 bash 脚本?

linux - 在 AWK 中使用 2 个关键字开始和停止打印

AWK:如何抑制默认打印