0

I am trying to compare file1 with file2 and redirect the difference to file 3 in Unix shell scripting.

I am getting below error:

syntax error near unexpected token `(' 
comm -2 -3 <(sort PATH/FILE1)  <(sort PATH/FILE2) > PATH/FILE3

data in file1:

A
B
D
C

Data in file 2:

A
C

Output expected in file3:

B
D
melpomene
  • 84,125
  • 8
  • 85
  • 148
mesh
  • 11
  • 5
  • Which shell are you using? – melpomene Jan 07 '18 at 13:29
  • i am using -bash – mesh Jan 07 '18 at 13:30
  • 2
    Can you prove it? – melpomene Jan 07 '18 at 13:31
  • I fired this command:echo $0 -bash – mesh Jan 07 '18 at 13:35
  • And what is the command that gives you the error? – melpomene Jan 07 '18 at 13:36
  • comm -2 -3 <(sort PATH/FILE1) <(sort PATH/FILE2) > PATH/FILE3 – mesh Jan 07 '18 at 13:37
  • 1
    I can't reproduce the problem. – melpomene Jan 07 '18 at 13:40
  • 1
    You tagged your question `sh` (as well as bash) . `echo $0` on the command-line will give `-bash`, but in a script should give the name of the script. Just because your command-line is running `bash` it doesn't mean that your script is. The syntax you show is valid in `bash` but not in `sh`. So, are you running this in a script? If so what `#!` line do you have, have you tried running it using `bash script-name`? Alternatively, maybe you are running a very old version, so `echo $BASH_VERSION`. – cdarke Jan 07 '18 at 15:27
  • One could get here if you were running bash in POSIX mode, but that's a thing that has to be explicitly turned on (and the answer is "don't do that"). – Charles Duffy Jan 07 '18 at 15:59
  • 1
    Any chance the platform might be Cygwin? I recall there being Cygwin releases where process substitution didn't work right, despite the bash release itself having appropriate support. – Charles Duffy Jan 07 '18 at 16:08

1 Answers1

0

When you run sort file you get the sorted output of that file, not a new file (name). But comm expects two files, not just file contents. You could either create temporary files,or try a different approach with uniq:

sort file1 file2 | uniq -u

First send both files' contents through sort, then the sorted contents get sent to uniq, which "reports or omits repeated lines". The -u tells it to print unique lines only.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • True, but much less efficient than `comm`, which *is* the right tool for this job. See [BashFAQ #36](https://mywiki.wooledge.org/BashFAQ/036). – Charles Duffy Jan 07 '18 at 15:49
  • The OP's syntax error indicates that their shell isn't really bash -- if they ran `sh scriptname` or ran a script with a `#!/bin/sh` shebang, they would get exactly the behavior in their question. – Charles Duffy Jan 07 '18 at 15:50
  • @CharlesDuffy I get the same error on bash, without script and shebang line. – Robert Jan 07 '18 at 15:52
  • To be clear: `<(...)` expands to a new filename, which when read from contains the output of the code in `...`. – Charles Duffy Jan 07 '18 at 15:52
  • Fortunately, we can test this due to the availability of online interpreters. See https://ideone.com/rrydwR, working perfectly. Could you please post a similar link with a test case that fails? – Charles Duffy Jan 07 '18 at 15:54
  • ...you can also put the OP's original code into http://shellcheck.net/, and note that it parses there perfectly. I'm curious a to how you managed to get a failure -- maybe you changed the spacing? `<()` is a very specific construct -- you can't change it to `< ()`, for instance, and have it still work. – Charles Duffy Jan 07 '18 at 15:55
  • BTW, see http://wiki.bash-hackers.org/syntax/expansion/proc_subst for guidance on the syntax in question, or the official bash manual's (terser) explanation at https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html – Charles Duffy Jan 07 '18 at 15:57
  • BTW, **the output from this is explicitly different from the `comm` line that the OP provided.** Their original code emits only lines that exist in file1 but not file2; your code, unlike theirs, also includes lines present in file2 but not file1. – Charles Duffy Jan 07 '18 at 16:01