Ruby script to check if a line is inside a file?

I have a CSV file of words and their frequencies, as well as a .txt file containing several interesting words, separated by newline characters.

I am looking for a way to verify that for each CSV row, the first column value (word) is also in the .txt file.

I think something similar to fgrep -x -f patternfile.txt data.csv except for only one CSV column, not the entire row?

+4
source share
5 answers

Using awk , you can do something like this -

 awk -v FS="," 'NR==FNR{a[$1]=$0;next} ($1 in a){print a[$1]}' csv_file txt_file 
  • Set the field separator to ,
  • Using the first column (word) of your csv file as an index, load the entire row into an array.
  • Check if the value of the text file is in the array.
  • If he then prints it

Test:

 [jaypal:~/Temp] cat csv_file jack,4 rabbit,10 cat,4 red,39 [jaypal:~/Temp] cat txt_file red rabbit cat [jaypal:~/Temp] awk -v FS="," 'NR==FNR{a[$1]=$0;next} ($1 in a){print a[$1]}' csv_file txt_file red,39 rabbit,10 cat,4 
+1
source

Hey, you can use FasterCSV to open and loop records.

  FasterCSV.foreach("#{file_path}") do |row| puts row[0]#row[0] is the first column only end 
+4
source

If we use bash, it will be:

 fgrep -x -f patternfile.txt data.csv | awk '{print $2}' 

as an easy way to get the second column from grep results

+2
source

To check if a file has a specific line in it at least once:

 IO.readlines(filename).map(&:chomp).include?("somestring") 
+1
source

To combine Phrogz's and yatish answers :

 require 'csv' words = File.readlines("words.txt").map(&:chomp) CSV::foreach("data.csv") {|row| puts row.to_csv if words.include?(row[0])} 
0
source

Source: https://habr.com/ru/post/1393111/


All Articles