awk的无与伦比的空白文件 [英] awk unmatched with blank file

查看:177
本文介绍了awk的无与伦比的空白文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到一个奇怪的行为问题与 AWK

Recently I've encountered a strange behavioral problem with awk

说我有两个文件中的一个空白文件&安培;另一个是填充数据

say I have two files one with blank file & the another is with populated data

所以让我采用一个简单的无与伦比的code

so let me apply a simple unmatched code

awk -v var=0  'NR==FNR{a[$var]++;next} !($var in a)' file1 file2

file1

&安培;

file 2
a
b
v

将返回的地方,因为它应该返回所有的内容在文件中2空白数据。
有人可以解释我如何克服这个问题?

it will return blank data where as it is supposed to return all the content in file 2. can someone explain me how to overcome this issue?

推荐答案

有没有文件1 的任何数据,因此整体备案号永远不会改变,所以 FNR == NR 整个文件2 。我不知道有一个简单的方法来解决这个问题,无论是。

There isn't any data in file1, so the overall record number never changes, so FNR == NR throughout file2. I'm not sure there's an easy way to fix that, either.

您甚至不能使用 BEGIN 块记录当前的文件名,并当场更改文件名时。对于 AWK <的POSIX规范/ A>说:

You can't even use a BEGIN block to record the current file name and spot when the file name changes. The POSIX specification for awk says:

文件名
      当前输入文件的路径名。在BEGIN操作的价值是不确定的。在END操作的值应处理的最后一个输入文件的名称

FILENAME A pathname of the current input file. Inside a BEGIN action the value is undefined. Inside an END action the value shall be the name of the last input file processed.

我觉得你最好的选择很可能是比较文件名 ARGV [1]

I think your best bet is likely to be comparing FILENAME with ARGV[1]:

awk -v var=0 'FILENAME==ARGV[1] {a[$var]++;next} !($var in a)' file1 file2

这篇关于awk的无与伦比的空白文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆