Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error on file name when using awk to compare two files

Tags:

bash

unix

awk

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.

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 -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.

like image 810
Djamillah Avatar asked Jan 20 '26 16:01

Djamillah


1 Answers

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.

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

like image 127
ShellFish Avatar answered Jan 23 '26 13:01

ShellFish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!