So I have a .csv file containing about 1000+ lines. The file consists of variety of movies; in the first column is the name, in the second is the rating (PG-13, R, etc), in the fifth is the year it was released... I basically take some user input (which in my script are defined as the user's search criteria) such as the year, score and rating they prefer. Now I want to print a random line from the file only if that line's fields have the same value as the user's preferences. Like if I were to enter the year 2001 and the rating R and the score 84,it would only print a line if that line has the corresponding column equal to the previously entered input. I'm completely lost on how to do this. I thought about this:
awk -F "," 'NR == '$rline' {if ($5 -eq "$yearpref1") {print $1 ", " $5} else {print "Sorry"}}' netflixNoDup.csv
which prints one random line, however the year isn't equal to (which in the csv file is $5) $yearpref1 (the variable that contains the user's year preference)... this awk command always just prints a random line.
How can I make it print only if the field is equal to the user's preference and continue to look for lines until it finds a line with the preferred year?
"$yearpref1"
will be a literal string here (use-v
to pass shell variables, or make use of theENVIRON
array), and-eq
is not an awk operator, so it will be treated as arithmetic expression- 0
print a random line from the file
- do you literally mean the output has to be random or are you OK with getting the first line that matches output?