如何使用awk过滤目录中的多个文件? [英] How to do filtering of multiple files in a directory using awk?

查看:75
本文介绍了如何使用awk过滤目录中的多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据黑名单文件过滤目录中的多个文件当我使用无循环的单行脚本时,效果很好...

I am trying to filter multiple files in a directory based on a blacklist file when I used a single line without loop the script works fine...

 awk >rest.txt 'NR==FNR{_[$1];next}!($1 in _)' FS=" " blist_file.txt main_file.txt

但是当我添加循环时,出现错误,这可能是什么问题?

but when I add a loop, i get error, what could be wrong?

$ for f in main_file* do awk >res_$f 'NR==FNR{_[$1];next}!($1 in _)' FS=" " blist_file.txt $f; done
-bash: syntax error near unexpected token `>

推荐答案

您缺少分号.这个:

for f in main_file* do

在语法上应该是这样:

for f in main_file*; do

但是当然这是错误的方法-只需致电awk一次即可

but of course this is the wrong approach anyway - just call awk once:

awk '
    NR==FNR {blist[$1]; next }
    FNR==1 {close(out); out="res_" FILENAME }
    !($1 in blist) { print > out }
' blist_file.txt main_file*.txt

您无需将FS设置为" ,因为这是默认值,并且只要知道使用"_"作为变量名都是好主意-切勿使用该源再次!

You don't need to set FS to " " as that is the default value and wrt wherever you learned that using "_" as a variable name is a good idea - never use that source again!

这篇关于如何使用awk过滤目录中的多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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