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?

  • This would be a lot easier, readable and less error-prone if you did it in Python. Are you interested in that solution or awk only? I think you can first grep for lines that match the preference, then do the random sample.
    – Bagalaw
    Nov 10, 2019 at 21:58
  • "$yearpref1" will be a literal string here (use -v to pass shell variables, or make use of the ENVIRON array), and -eq is not an awk operator, so it will be treated as arithmetic expression - 0 Nov 10, 2019 at 22:41
  • edit your question to include concise, testable sample input and expected output so we can best help you.
    – Ed Morton
    Nov 11, 2019 at 13:57
  • When you say 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?
    – Ed Morton
    Nov 11, 2019 at 14:06

1 Answer 1


Untested and just guessing at field numbers since you didn't provide any sample input/output but you'll want something like this:

awk -F ',' -v OFS=', ' -v year="$yearpref1" -v rating="$rating" -v score="$score" '
(year == $5) && (rating == $9) && (score == $23) { print $1, $5; f=1; exit }
END { if (!f) print "Sorry" }
' netflixNoDup.csv

Note that that prints the first matching line, not a random matching line.

  • Hey thanks for the reply. Is there any way to get this to print a random line though?
    – user381358
    Nov 16, 2019 at 0:39
  • Simplest way is print them all and pipe to shuf -n1
    – Ed Morton
    Nov 16, 2019 at 0:41
  • You mind explaining me how this awk command works? I haven't seen this format before so I'm a little confused
    – user381358
    Nov 16, 2019 at 0:51
  • Not sure where it's different from any other common and garden awk script. It's testing if a condition is true ((year == $5) && (rating == $9) && (score == $23)) and if so executing an action ({ print $1, $5; f=1; exit }). When it's quitting it prints "Sorry" if f wasn't previously set by that action (i.e. the condition was never true). Anything in particular throwing you off?
    – Ed Morton
    Nov 16, 2019 at 1:08
  • Yeah I understood that. Thinking more about my question, what I'm finding difficult to understand in particular are the "-v" used. What are they for? Also, is it necessary to put "exit"?
    – user381358
    Nov 16, 2019 at 1:32

You must log in to answer this question.