Awk跳过空白行。

时间:2021-04-19 21:53:46

The output of my script is tab delimited using awk as :

我的脚本的输出是用awk作为标签分隔的:

awk -v variable=$bashvariable  '{print variable"\t single\t" $0"\t double"}' myinfile.c

The awk command is run in a while loop which updates the variable value and the file myinfile.c for every cycle. I am getting the expected results with this command . But if the inmyfile.c contains a blank line (it can contain) it prints no relevant information. can I tell awk to ignore the blank line ?

awk命令在while循环中运行,该循环更新变量值和文件myinfile。c每周期。通过这个命令,我得到了预期的结果。但如果inmyfile。c包含一个空行(它可以包含)它不打印相关信息。我可以告诉awk忽略空行吗?

I know it can be done by removing the blank lines from myinfile.c before passing it on to awk . I am in knowledge of sed and tr way but I want awk to do it in the above mentioned command itself and not a separate solution as below or a piped one.

我知道可以从myinfile中删除空行。在把它传给awk之前。我知道sed和tr的方法,但我希望awk在上面提到的命令本身,而不是一个单独的解决方案,如下或一个管道。

sed '/^$/d' myinfile.c
tr -s "\n" < myinfile.c

Thanks in advance for your suggestions and replies.

谢谢您的建议和回复。

4 个解决方案

#1


40  

There are two approaches you can try to filter out lines:

有两种方法可以尝试过滤线:

awk 'NF' data.txt

and

awk 'length' data.txt

Just put these at the start of your command, i.e.,

把这些放在命令的开头,例如,

awk -v variable=$bashvariable 'NF { print variable ... }' myinfile

or

awk -v variable=$bashvariable 'length { print variable  ... }' myinfile

Both of these act as gatekeepers/if-statements.

这两种行为都是“看门人”的说法。

The first approach works by only printining out lines where the number of fields (NF) is not zero (i.e., greater than zero).

第一种方法只打印出字段数(NF)不为零的行。大于零)。

The second method looks at the line length and acts if the length is not zero (i.e., greater than zero)

第二个方法查看行长度,并在长度不为0时执行。大于零)

You can pick the approach that is most suitable for your data/needs.

您可以选择最适合您的数据/需要的方法。

#2


9  

You could just add

你可以添加

/^\s*$/ {next;}  

To the front of your script that will match the blank lines and skip the rest of the awk matching rules. Put it all together:

在脚本的前面,将匹配空行并跳过awk匹配规则的其余部分。把这一切放在一起:

awk -v variable=$bashvariable '/^\s*$/ {next;} {print variable"\t single\t" $0"\t double"}' myinfile.c

#3


2  

may be you could try this out:

也许你可以试试这个:

awk -v variable=$bashvariable  '$0{print variable"\t single\t" $0"\t double"}' myinfile.c

#4


1  

Try this:

试试这个:

awk -v variable=$bashvariable '/^.+$/{print variable"\t single\t" $0"\t double"}' myinfile.c

#1


40  

There are two approaches you can try to filter out lines:

有两种方法可以尝试过滤线:

awk 'NF' data.txt

and

awk 'length' data.txt

Just put these at the start of your command, i.e.,

把这些放在命令的开头,例如,

awk -v variable=$bashvariable 'NF { print variable ... }' myinfile

or

awk -v variable=$bashvariable 'length { print variable  ... }' myinfile

Both of these act as gatekeepers/if-statements.

这两种行为都是“看门人”的说法。

The first approach works by only printining out lines where the number of fields (NF) is not zero (i.e., greater than zero).

第一种方法只打印出字段数(NF)不为零的行。大于零)。

The second method looks at the line length and acts if the length is not zero (i.e., greater than zero)

第二个方法查看行长度,并在长度不为0时执行。大于零)

You can pick the approach that is most suitable for your data/needs.

您可以选择最适合您的数据/需要的方法。

#2


9  

You could just add

你可以添加

/^\s*$/ {next;}  

To the front of your script that will match the blank lines and skip the rest of the awk matching rules. Put it all together:

在脚本的前面,将匹配空行并跳过awk匹配规则的其余部分。把这一切放在一起:

awk -v variable=$bashvariable '/^\s*$/ {next;} {print variable"\t single\t" $0"\t double"}' myinfile.c

#3


2  

may be you could try this out:

也许你可以试试这个:

awk -v variable=$bashvariable  '$0{print variable"\t single\t" $0"\t double"}' myinfile.c

#4


1  

Try this:

试试这个:

awk -v variable=$bashvariable '/^.+$/{print variable"\t single\t" $0"\t double"}' myinfile.c