2

Here's an example of a few lines of my CSV file:

movieID,actorID,actorName,ranking
1,don_rickles,Don Rickles,3
1,jack_angel,Jack Angel,6
1,jim_varney,Jim Varney,4
1,tim_allen,Tim Allen,2
1,tom_hanks,Tom Hanks,1
1,wallace_shawn,Wallace Shawn,5

I would like to remove all rows that have a ranking of > 4, so far I've been trying use this awk line:

awk -F ','  'BEGIN {OFS=","} { if (($4) < 5)  print }' file.csv > file_out.csv

It should print all the rows with a ranking (4th column) of less than 5 to a new file. I can't tell exactly what this line actually does, but it's not what I want. can someone tell me where I've gone wrong with that line?

7
  • guess what does <5 mean? Commented Jan 8, 2016 at 17:02
  • it should mean if the value of column 4 is less than 5. Commented Jan 8, 2016 at 17:03
  • yep , if this line worked, you change <5 into >4 should go, right? Commented Jan 8, 2016 at 17:04
  • @Kent wouldn't that output all lines with a value of >4 into file_out.csv? in my example above, it would print 1,jack_angel,Jack Angel,6 and 1,wallace_shawn,Wallace Shawn,5 into file_out.csv Commented Jan 8, 2016 at 17:06
  • and the last line too (5>4). what do you expect then? Commented Jan 8, 2016 at 17:07

1 Answer 1

3

Instead of deleting the records, think of which ones you're going to print. I guess it's <=4. In idiomatic awk you can write this as

$ awk -F, '$4<=4' file

1,don_rickles,Don Rickles,3
1,jim_varney,Jim Varney,4
1,tim_allen,Tim Allen,2
1,tom_hanks,Tom Hanks,1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.