Can you please help to fix this script?

$ private=False
$ min_num_followers=100
$ max_num_followers=1000
$ awk -v private="$private" '$11=private' ExportData_followers_memoryweaver_37677682_10.csv  |  awk -v min_num_followers="$min_num_followers" '$9 > min_num_followers'| awk -v max_num_followers="$max_num_followers" '$9 < max_num_followers'  |awk -F"," '{x+=$8} END {print x}'

I want to get the rows in which the 9th column is > min_num_followers and < max_num_followers and also 11th column is private. enter image description here

**I am not biased towards using awk anyways

share|improve this question
1  
Please copy-paste actual content, not screenshot. Also add your desired output. – heemayl Mar 7 '17 at 3:34
    
@heemayl can't do so, the content is private! – Mona Jalal Mar 7 '17 at 3:35
2  
Then post those as masked. – heemayl Mar 7 '17 at 3:46
up vote 1 down vote accepted

Something like awk -F, '$9 > 500 && $9 < 1000 && $11 == "True"' should work

share|improve this answer
    
Thanks, this worked awk -F, '$9 > 500 && $9 < 1000 && $11 = "False"' ExportData_followers_sabaasafari_398725412_10.csv | wc -l – Mona Jalal Mar 7 '17 at 3:41
    
by the way seems it is not applying it on the correct columns. Does 9 pertain to I in CVS? – Mona Jalal Mar 7 '17 at 3:47
    
@MonaJalal here it is using comma as separator. If you want to use the pipe as a separator instead, then change -F, to -F| – Markasoftware Mar 7 '17 at 4:06
1  
@JJoao thanks, fixed it – Markasoftware Mar 28 '17 at 23:31

You don't need multiple awk commands - you can chain the logical conditions with && (or || if the logic demands it).

Also you need to replace $11=private (assignment) by $11==private (logical test). So something like this should work:

awk -F"," -v private="$private" -v min_num_followers="$min_num_followers" -v max_num_followers="$max_num_followers" '
  $11==private && $9 > min_num_followers && $9 < max_num_followers
' ExportData_followers_memoryweaver_37677682_10.csv

If you want the output to be comma-separated, set OFS = "," either in the body of the awk expression or on the command line e.g.

awk -F"," -v private="$private" -v min_num_followers="$min_num_followers" -v max_num_followers="$max_num_followers" '
  $11==private && $9 > min_num_followers && $9 < max_num_followers
' OFS="," ExportData_followers_memoryweaver_37677682_10.csv
share|improve this answer
    
so in the output, commas are dropped, how can I maintain them in the awk -F"," '$9 > 100 && $9 < 1000 && $11 = "False"' ? – Mona Jalal Mar 7 '17 at 22:14
    
@MonaJalal the output field separator is called OFS so if you want comma-separated output you must set OFS="," either inside the expression or on the command line. As I mentioned above, you should use $11 == "False" not $11 = "False" if you want to make it conditional on the value of $11. – steeldriver Mar 7 '17 at 22:52

Just confirming that these two both worked:

$ awk -F"," '$9 > 100 && $9 < 1000 && $11 == "False"' ExportData_followers_reddit_1702003595_10.csv | wc -l
1089
$ gawk -F"," '$9 > 100 && $9 < 1000 && $11 == "False"' ExportData_followers_reddit_1702003595_10.csv | wc -l
1089
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.