阅读背景:

awk脚本中的$ 0和$ 1是多少?

来源:互联网 

in awk:

在awk:

 1  tolower(

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


) ~ /mary/ { print "CI Record: "

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


; } 2

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


!~ /Mary/ { print "Not Mary: "

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


; } 3

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


== "Mary" { print "Mary Record: "

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


; } 1 tolower(

in awk:

在awk:

 1  tolower($1) ~ /mary/ { print "CI Record: " $0; }
 2  $0 !~ /Mary/ { print "Not Mary: " $0; }
 3  $1 == "Mary" { print "Mary Record: " $0; }

Why 1 and 3 compare $1 and 2 using $0?

为什么1和3比较$ 1和2使用$ 0?

3 个解决方案

#1


7  

Actually example # 2 is using a regex because of this syntax

实际上,由于这种语法,示例#2正在使用正则表达式

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

这意味着在您的示例中,如果在整行($ 0)中找不到文字文本Mary,则执行awk代码。

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

而$ 1 ==“Mary”正在文字文本Mary和字段#1($ 1)之间进行直接比较。

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.

最后tolower($ 1)〜/ mary /再次使用字段#1上的ignre-case正则表达式匹配,这意味着如果$ 1有文本mary(ignore-case),则执行其余的awk代码。

#2


18  

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

在awk中,$ 0是整个参数行,而$ 1只是由空格分隔的参数列表中的第一个参数。因此,如果我通过awk放“玛丽有一只小羊羔”,1美元是“玛丽”,但0美元是“玛丽有一只小羊羔”。第二行是试图在给予awk的整行中找到子串“Mary”。

#3


2  

From the description in your link (emphasis mine):

从链接中的描述(强调我的):

The expression is generally either one of the fields or the result of an operation on one of the fields. For example, the following AWK filter rules show, respectively, how to compare the first field to “mary” in a case-insensitive fashion, how to match all records that do not contain “Mary”, and how to do an exact comparison of the first field against “Mary”:

表达式通常是其中一个字段或其中一个字段上的操作结果。例如,以下AWK过滤规则分别显示如何以不区分大小写的方式将第一个字段与“mary”进行比较,如何匹配不包含“Mary”的所有记录,以及如何进行精确比较反对“玛丽”的第一场:

So breaking it down:

所以打破它:

First one:

第一:

 how to compare the first field to “mary” in a case-insensitive fashion

Because it's comparing the first field, it uses $1

因为它比较第一个字段,所以它使用1美元

how to match all records that do not contain “Mary”,

如何匹配所有不包含“Mary”的记录,

Since it's comparing all records, it uses $0

由于它正在比较所有记录,因此它使用0美元

Third one

第三个

and how to do an exact comparison of the first field against “Mary”:

以及如何对第一个字段与“Mary”进行精确比较:

Comparing first field again, so it's using $1.

再次比较第一个字段,所以它使用$ 1。


) ~ /mary/ { pr



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: