In Bash, I want to compare the fields of 2 different CSVs (field 2 of file1 and field 3 of file2):
diff <(cut -d, -f2 file1) <(cut -d, -f3 file2)
I tried to implement this more generally in Ruby:
def identical_files?(file1, field1, file2, field2)
%x{diff <(cut -d, -f#{field1} #{file1}) <(cut -d, -f#{field2} #{file2})}.blank?
end
Printing the output of the %x{} block, I see sh: Syntax error: "(" unexpected. Does I/O redirection not work when running shell commands within Ruby? Is this because it’s only supported by bash but not sh?
It doesn’t work because, as the error you’re getting indicates, Ruby shells out to sh, not Bash. And, of course, sh does not support that syntax.
You can instead call Bash explicitly: