Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby script to check if a string is within a file?

Tags:

bash

ruby

csv

awk

I have CSV file of words and their frequencies as well as a .txt file containing several words of interest, separated by newlines.

I'm looking for a way to check if, for every row of the CSV, the first column value (the word) is also in the .txt file.

I guess something similar to fgrep -x -f patternfile.txt data.csv except with only 1 column of the CSV rather than the entire row?

like image 306
Amy Li Avatar asked Jan 17 '23 16:01

Amy Li


1 Answers

Hey You can use FasterCSV to open and loop thru the records.

        FasterCSV.foreach("#{file_path}") do |row| 
        puts row[0]#row[0] is the first column only
        end 
like image 86
yatish mehta Avatar answered Jan 28 '23 13:01

yatish mehta