0

I want to compare with online users inside freeradius database and users inside mikrotik devices , so I retrieved online users from freeradius and insert them inside file1, get online users from mikrotik and insert them inside file2, now I want to get the users that are found in file2 but not found inside file1 so I used this code

I am trying to get users that are found in file2 but not found in file 1.

 comm -23 <(sort < file2) <(sort  file1)

but I had this error

Syntax error: "(" unexpected.

So where is my error?

actually I don't know much things about bash and shell , so please help me

tripleee
  • 175,061
  • 34
  • 275
  • 318
Abobaker EngIt
  • 37
  • 1
  • 10
  • Seems like you have a `<` too much for file2 – kvantour Feb 15 '19 at 15:03
  • 1
    What shell are you using? Not all common ones support the `<(command)` syntax - including dash, which is used for Ubuntu's `/bin/sh`. – Shawn Feb 15 '19 at 15:04
  • 1
    Your immediate problem is probably that you used `sh` to execute a Bash script. See the second duplicate for details. – tripleee Feb 15 '19 at 15:12

1 Answers1

0

Assuming your files contain 1 username per line in file1, a simple one liner like the below should do what you need, without having to sort the files :

while read line; do if grep -q "$line" file2; then echo "user in both :" "$line"; fi; done < file1
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • @tripleee fixed – nullPointer Feb 15 '19 at 15:09
  • That's also going to be far more inefficient if the files are of any significant size. – Shawn Feb 15 '19 at 15:19
  • @Shawn, ok but are you sure that sorting 2 big files instead before comparing them would be more efficient ? – nullPointer Feb 15 '19 at 15:22
  • Two `O(N log N)` sorts and one `O(N)` comparison of the results vs basically an `O(N^2)` loop? Wouldn't be surprised, no. – Shawn Feb 15 '19 at 15:29
  • It's not an `O(N^2)` loop scan. Only 1 file is scanned, once : _I am trying to get users that are found in file2 but not found in file 1_ – nullPointer Feb 15 '19 at 15:36
  • 1
    For every line in file1 you look at up to every line in file2. It's a nested loop. – Shawn Feb 15 '19 at 15:37
  • There's also a potential problem if any line in file1 has regexp metacharacters, but that's trivial to fix. – Shawn Feb 15 '19 at 15:40
  • 1
    @funkyjelly, ...short answer is that GNU sort is aggressively optimized; f/e, if you have inputs too large to fit in memory, it sorts subsets of the file into different temporary files per each, and does a merge-sort to combine them together. If you have inputs big enough to need that kind of optimization, running grep once per line is almost unthinkably inefficient in comparison (and the `awk`-based alternatives are going to run one out of memory quickly). – Charles Duffy Feb 15 '19 at 15:53
  • @Charles thanks for the info. I was not aware of this sort optimization you just explained. – nullPointer Feb 15 '19 at 15:56