bash awk - 如果列 = null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15665637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:30:31  来源:igfitidea点击:

awk - if column = null

bashawk

提问by Numpty

Using awk, how would you assign 'null' a value to read?

使用 awk,你将如何分配 'null' 一个值来读取?

I'm sure there's a set character for this, I just can't find it.

我确定这有一个固定的字符,我只是找不到它。

For example,

例如,

I've got a string of awk like this:

我有一串这样的awk:

awk '
==24{print "stuff"}
==23{print "stuff"}
'

I need to know how to account for blank colums using the same format so that if $3 = blank {print "stuff"}

我需要知道如何使用相同的格式来计算空白列,以便如果 $3 = blank {print "stuff"}

Thanks!

谢谢!

回答by Gilles Quenot

Try doing this :

尝试这样做:

awk '
    ==24{print "stuff"}
    ==23{print "stuff"}
    !{print "null"}
' file.txt

If you need to process $3if it's zero (falsefor awk), try doing this :

如果您需要处理$3它是否为零(falsefor awk),请尝试执行以下操作:

! &&  != 0{print "null"}

回答by Keith Thompson

In default awk processing, there's no such thing as a "blank" column.

在默认的 awk 处理中,没有“空白”列这样的东西。

Fields are delimited by whitespace, i.e., by one or morewhitespace characters (tabs and spaces, basically). So given this input:

字段由空格分隔,即由一个或多个空格字符(基本上是制表符和空格)分隔。所以给定这个输入:

this that the_other
foo       bar

for the first line $1, $2, and $3are this, that, and the_other, respectively, but for the second line baris $2, regardless of how many blanks there are between the first and second fields.

对于第一行$1$2以及$3thisthat,和the_other,分别,但对于第二行bar$2不管有多少坯料有所述第一和第二场之间。

You can have empty fields if you specify a different field separator:

如果指定不同的字段分隔符,则可以有空字段:

$ ( echo 'this:that:the_other' ; echo 'foo::bar' ) | awk -F: '{print }'
the_other
bar

Or, if you prefer to set the field separator in the script itself:

或者,如果您更喜欢在脚本本身中设置字段分隔符:

$ ( echo 'this:that:the_other' ; echo 'foo::bar' ) | \
    awk 'BEGIN { FS = ":" } {print }'
the_other
bar

But you can use a regular expression as the field separator:

但是您可以使用正则表达式作为字段分隔符:

$ ( echo 'this that the_other' ; echo 'foo  bar' ) | \
  awk 'BEGIN { FS = "[ ]" } {print }'
the_other
bar

(Some veryold Awk implementations might not support it regular expressions here.)

(一些非常老的 awk 实现可能不支持这里的正则表达式。)

The regular expression "[ ]"doesn't get the same special treatment that the space character does.

正则表达式"[ ]"没有得到与空格字符相同的特殊处理。

References to the GNU Awk manual:

参考 GNU Awk 手册:

Default field splitting:

默认字段拆分

Fields are normally separated by whitespace sequences (spaces, TABs, and newlines), not by single spaces. Two spaces in a row do not delimit an empty field. The default value of the field separator FSis a string containing a single space, " ". If awk interpreted this value in the usual way, each space character would separate fields, so two spaces in a row would make an empty field between them. The reason this does not happen is that a single space as the value of FSis a special case -- it is taken to specify the default manner of delimiting fields.

If FSis any other single character, such as ",", then each occurrence of that character separates two fields. Two consecutive occurrences delimit an empty field. If the character occurs at the beginning or the end of the line, that too delimits an empty field. The space character is the only single character that does not follow these rules.

字段通常由空格序列(空格、制表符和换行符)分隔,而不是由单个空格分隔。一行中的两个空格不会分隔空字段。字段分隔符的默认值FS是一个包含单个空格的字符串," "。如果 awk 以通常的方式解释这个值,每个空格字符将分隔字段,因此一行中的两个空格将在它们之间形成一个空字段。这不会发生的原因是作为值的单个空格FS是一种特殊情况——它被用来指定分隔字段的默认方式。

如果FS是任何其他单个字符,例如",",则该字符的每次出现都会分隔两个字段。两次连续出现划定一个空字段。如果字符出现在行的开头或结尾,那也将分隔空字段。空格字符是唯一不遵循这些规则的单个字符。

and Using Regular Expressions to Separate Fields.

使用正则表达式来分隔字段

But be careful with this; either you'll have to modify the file to use a different separator, or your parsing will be sensitive to the number of blanks between fields (foo bar(with one blank) will be distinct from foo bar(with two blanks)).

但要小心;要么您必须修改文件以使用不同的分隔符,要么您的解析将对字段之间的空格数敏感(foo bar(一个空格)与foo bar(两个空格)不同)。

Depending on your application, you might consider parsing lines by column number rather than by awk-recognized fields.

根据您的应用程序,您可能会考虑按列号而不是 awk 识别的字段来解析行。