使用awk比较两个文件时文件名上的语法错误

时间:2022-11-24 22:43:18

I'm trying to use awk to print the lines contained in one file2 if the numbers in column 1 or 5 is in file1, but I'm getting a syntax error that I don't understand.

我试图使用awk来打印一个文件2中包含的行,如果第1列或第5列中的数字在file1中,但我得到的语法错误我不明白。

My input is:

我的意见是:

file1.dat

1
3
4
6
8
13
14
25

etc...

file2.dat

2 GLU 1 - 3 ARG 2
24 ASP 2 - 12 LYS 1
3 ASP 1 - 25 ARG 2
7 LYS 2 - 17 GLU 2
18 ARG 1 - 13 GLU 2

etc...

In this case I want the output

在这种情况下,我想要输出

2 GLU 1 - 3 ARG 2
3 ASP 1 - 25 ARG 2
18 ARG 1 - 13 GLU 2

I tried to do this with the following awk-line

我尝试使用以下awk-line执行此操作

awk -F 'NR==FNR{a[$1||$5]++;next} (a[$1||$5])' file1.dat file2.dat

but I get the error

但我得到了错误

awk: file1.dat
awk:      ^syntax error

Does anyone know what is causing this error? I have tried to put the file names into variables but that produces the same error.

有谁知道导致此错误的原因是什么?我试图将文件名放入变量但产生相同的错误。

1 个解决方案

#1


You have supplied the -F flag for field separator. The next argument is therefore the fields separator (which is your script because you didn't supply a seperator). So awk takes the first file as the script itself.

您已为字段分隔符提供了-F标志。因此,下一个参数是字段分隔符(这是您的脚本,因为您没有提供分隔符)。所以awk将第一个文件作为脚本本身。

Try to either drop the -F or adding a separator e.g. awk -F '[ \t]*' '...' file1 file2.

尝试删除-F或添加分隔符,例如awk -F'[\ t] *''...'file1 file2。

#1


You have supplied the -F flag for field separator. The next argument is therefore the fields separator (which is your script because you didn't supply a seperator). So awk takes the first file as the script itself.

您已为字段分隔符提供了-F标志。因此,下一个参数是字段分隔符(这是您的脚本,因为您没有提供分隔符)。所以awk将第一个文件作为脚本本身。

Try to either drop the -F or adding a separator e.g. awk -F '[ \t]*' '...' file1 file2.

尝试删除-F或添加分隔符,例如awk -F'[\ t] *''...'file1 file2。