ruby - Grep and print list of strings from two files? -
i have 2 large files , want take list of strings (in third file) , print each string exists in first large file not other. want run on command line 1 liner.
#snippet of big_file_1 ahello.$ line blahblah llo no #snippet of big_file_2 123 line blahblah # list_of_strings file hello address name # expected output => hello i've tried following 2 options. first gives me shell error, second results in no output. hello in first file , not second i'm expecting output. running in irb, second option's if() returns true. why not getting puts output?
ruby -ne 'puts $_ if ((`grep #{$_} big_file_1`.length >0) && !(`grep #{$_} big_file_2`.length >0))' < list_of_strings ruby -ne 'puts $_ if ((`grep $_ big_file_1`.length >0) && !(`grep $_ big_file_2`.length >0))' < list_of_strings
the first gives me shell error,
this because -n ruby option leaves newline on $_. answers other questions.
to fix this, chomp! off:
ruby -ne '$_.chomp!; puts $_ if ((`grep #{$_} big_file_1`.length >0) && \ !(`grep #{$_} big_file_2`.length >0))' < list_of_strings output:
hello
Comments
Post a Comment