awk unmatched with blank file

*爱你&永不变心* 提交于 2019-12-24 04:01:17

问题


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

so let me apply a simple unmatched code

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

say

file1

&

file 2
a
b
v

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:


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.

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:

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.

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


来源:https://stackoverflow.com/questions/30600805/awk-unmatched-with-blank-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!