1

I am trying to sum certain numbers in a column using awk. I would like to sum just column 13 (love_count) of the "Barack Obama" to get the total. I need to sum the love_count whenever the word "Barack Obama" is contained in a row. Here's an example of the input file:

    page_name,post_id,page_id,post_name,message,description,caption,post_type,status_type,likes_count,comments_count,shares_count,love_count,wow_count,haha_count,sad_count,thankful_count,angry_count,post_link,picture,posted_at
abc-news,86680728811_272953252761568,86680728811,Chief Justice Roberts Responds to Judicial Ethics Critics,Roberts took the unusual step of devoting the majority of  his annual  report to the issue of judicial ethics.,PAUL J. RICHARDS/AFP/Getty Images Chief Justice John Roberts issued a ringing endorsement Saturday night of his colleagues_ ability to determine when they should step down from a case because of a conflict of interest. _I have complete confidence in the capability of my colleagues to determine when ...,abcnews.go.com,link,shared_story,61,27,12,0,0,0,0,0,0,http://abcnews.go.com/blogs/headlines/2011/12/chief-justice-roberts-responds-to-judicial-ethics-critics/,https://external.xx.fbcdn.net/safe_image.php?d=AQAPXteeHLT2K7Rb&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FPolitics%2Fgty_chief_justice_john_roberts_jt_111231_wblog.jpg&cfs=1&sx=108&sy=0&sw=269&sh=269,1/1/12 0:30
abc-news,86680728811_273859942672742,86680728811,With Reservations .. Obama Signs Act to Allow Detention of Citizens,Do you agree with the new law?,In his last official act of business in 2011 .. President Barack Obama signed the National Defense Authorization Act from his vacation rental in Kailua .. Hawaii. In a statement .. the president said he did so with reservations about key provisions in the law _ including a controversial component that wou...,abcnews.go.com,link,shared_story,120,523,171,0,0,0,0,0,0,http://abcnews.go.com/blogs/politics/2011/12/with-reservations-obama-signs-act-to-allow-detention-of-citizens/,https://external.xx.fbcdn.net/safe_image.php?d=AQB-dNUknp57I-gC&w=112&h=112&url=http%3A%2F%2Fa.abcnews.com%2Fimages%2FPolitics%2Fabc_obama_weekly_111231_wl.jpg&cfs=1&sx=25&sy=0&sw=112&sh=112,1/1/12 1:08
abc-news,86680728811_10150499874478812,86680728811,Wishes For 2012 to Fall on Times Square,Some pretty cool confetti will rain down on New York City celebrators.,The wishes of thousands of people will flutter down from New York City_s buildings and descend on Times Square when the iconic ball drops tomorrow...,abcnews.go.com,link,published_story,271,31,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/headlines/2011/12/wishes-for-2012-to-fall-on-times-square/,https://external.xx.fbcdn.net/safe_image.php?d=AQAbTSWm1WlXInTf&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FUS%2Fap_new_years_notes_dm_111230_wblog.jpg&cfs=1,1/1/12 2:00
abc-news,86680728811_244555465618151,86680728811,Mitt Romney Vows to Veto Dream Act if President,NULL,Eric Gay/AP Photo SIOUX CITY .. Iowa _ Mitt Romney explicitly stated today that if he is elected president he would veto the Dream Act .. legislation that would give permanent residency to some illegal immigrants who met certain criteria .. such as having proof that they entered the country before age 16 ...,abcnews.go.com,link,shared_story,140,188,23,0,0,0,0,0,0,http://abcnews.go.com/blogs/politics/2011/12/mitt-romney-vows-to-veto-dream-act-if-president/,https://external.xx.fbcdn.net/safe_image.php?d=AQDlNqSqBc-j4Me5&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FPolitics%2Fap_mitt_romney_iowa_lemars_lt_111231_wblog.jpg&cfs=1,1/1/12 2:35
abc-news,86680728811_252342804833247,86680728811,NY Pharmacy Shootout Leaves Suspect .. ATF Agent Dead,The pharmacy was held up by a man seeking prescription medication. ,A shootout at a suburban New York family pharmacy left a suspected robber and an off-duty federal agent dead in a confused shootout that took place as the robber fled .. authorities said. Charlies Pharmacy in Seaford .. Long Island .. N.Y. was held up by a man seeking prescription medication this afterno...,abcnews.go.com,link,shared_story,59,51,34,0,0,0,0,0,0,http://abcnews.go.com/US/ny-pharmacy-shootout-leaves-suspect-atf-agent-dead/story?id=15267287,https://external.xx.fbcdn.net/safe_image.php?d=AQCJcS3Q0a_XjHrZ&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FUS%2Fap_charlie_pharmacy_long_island_shooting_lt_111231_me.jpg&cfs=1,1/1/12 3:36
abc-news,86680728811_200661383359612,86680728811,The World Rings in 2012,NULL,Around the world .. countries celebrate the new year. ,abcnews.go.com,link,shared_story,498,52,108,0,0,0,0,0,0,http://abcnews.go.com/International/slideshow/world-rings-2012-15265811,https://external.xx.fbcdn.net/safe_image.php?d=AQCwlWVHT8nOc_TV&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FInternational%2Fgty_new_year_australia_sswm12_jt_111231_me.jpg&cfs=1,1/1/12 4:13
abc-news,86680728811_281125741936891,86680728811,Breaking: Magnitude 7.0 Quake His Japan .. No Tsunami Warning,There were no immediate reports of damage or injuries.,A magnitude 7.0 earthquake hits eastern Japan .. no danger of a tsunami,abcnews.go.com,link,shared_story,241,225,430,0,0,0,0,0,0,http://abcnews.go.com/International/wireStory/magnitude-70-quake-japan-tsunami-warning-15267916,NULL,1/1/12 6:00
abc-news,86680728811_10150500662053812,86680728811,PC .. TV Buyers to Collect $553M LCD Settlement,Were you an LCD screen early adopter? A settlement may be headed your way.,Samsung Electronics .. Sharp .. Epson Imaging Devices .. and four other electronics manufacturers based in Asia agreed to a $553 million settlement with...,abcnews.go.com,link,published_story,117,24,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/business/2011/12/pc-tv-buyers-to-collect-553m-lcd-settlement/,https://external.xx.fbcdn.net/safe_image.php?d=AQCIsWbPTyoRQ0hD&w=130&h=130&url=http%3A%2F%2Fa.abcnews.com%2Fimages%2FSite%2Fabc_default_image_mc.jpg&cfs=1,1/1/12 14:00
abc-news,86680728811_10150500969563812,86680728811,Coast Guard Passenger-Limit Rule Reflects Americans Weight Gain,As Americans get bigger .. passenger limits are becoming more restrictive.,The average U.S. adult weighs significantly more today than a few decades ago .. prompting the U.S. Coast Guard to implement nationwide regulations...,abcnews.go.com,link,published_story,124,96,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/health/2011/12/30/coast-guard-passenger-limit-rule-reflects-americans-weight-gain/,https://external.xx.fbcdn.net/safe_image.php?d=AQDhl6HdPjtIUV4g&w=130&h=130&url=http%3A%2F%2Fa.abcnews.com%2Fimages%2FGMA%2Fabc_gma_rev_change_111230_wl.jpg&cfs=1,1/1/12 17:00
abc-news,86680728811_10150501303143812,86680728811,WHO Deeply Concerned By Lab-Created Bird Flu Mutation,Researchers in the Netherlands have manipulated the virus to make it more transmissible among humans .. and it could potentially kill millions if released into the public.,The World Health Organization (WHO) says it is _deeply concerned_ about researchers creating a more contagious and fatal form of the H5N1 bird flu.,abcnews.go.com,link,published_story,124,232,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/health/2011/12/30/who-deeply-concerned-by-lab-created-bird-flu-mutation/,https://external.xx.fbcdn.net/safe_image.php?d=AQBlB_-viKEDbsL3&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fblogs%2Fhealth%2F2011%2F12%2F30%2Fwho-deeply-concerned-by-lab-created-bird-flu-mutation%2Fno_facebook_preview_picture.jpg&cfs=1,1/1/12 20:00
abc-news,86680728811_305275689511038,86680728811,Dead Birds Fall From Ark. Sky .. Again,Happy New Year? Not for These Birds,For the second year in a row .. blackbirds fell from the sky in small town.,abcnews.go.com,link,shared_story,225,251,283,0,0,0,0,0,0,http://abcnews.go.com/US/dead-blackbirds-fall-sky-beebe-arkansas-years-eve/story?id=15269793,https://external.xx.fbcdn.net/safe_image.php?d=AQBXL36Phfjr-k6z&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FUS%2Fabc_dead_birds3_jt_120101_me.jpg&cfs=1,1/1/12 21:16
abc-news,86680728811_10150501624593812,86680728811,Police kill Georgia Woman Who Sent Fake Bomb .. Severed Pigs Foot to GOP...,Police say she was shot dead after she took a swipe at an officer with the knife.,Womans notes charged NY State Sen. Ball .. Rep. Peter King with anti-Muslim bias.,abcnews.go.com,link,published_story,117,103,0,0,0,0,0,0,0,http://abcnews.go.com/US/georgia-woman-shot-police-sending-fake-wmd-package/story?id=15240887,https://external.xx.fbcdn.net/safe_image.php?d=AQBPH4g6LRPDKXNy&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2FUS%2Fgeorgia-woman-shot-police-sending-fake-wmd-package%2Fstory%3Fid%3D15240887%252Fno_facebook_preview_picture.jpg&cfs=1,1/1/12 23:00
abc-news,86680728811_10150501873973812,86680728811,Parents Arrested for Allegedly Posting Photos of Bound Kids on Facebook,One photo depicted a child hanging upside down from exercise equipment.,An Arizona couple has been arrested for allegedly posting photos of their two young children bound by duct tape.,abcnews.go.com,link,published_story,186,319,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/headlines/2011/12/parents-arrested-for-allegedly-posting-photos-of-bound-kids-on-facebook/,https://external.xx.fbcdn.net/safe_image.php?d=AQAS5CM38LmE4wsu&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FUS%2Fht_kayla_frankie_almuina_nt_111230_wblog.jpg&cfs=1,2/1/12 2:00
abc-news,86680728811_145945585518261,86680728811,Person of Interest Identified in Park Ranger Killing,Police said they are looking for Benjamin Colton Barnes .. in connection with the killing today of Ranger Margaret Anderson.,(The News Tribune .. Dean J. Koepfler/AP Photo) The person of interest identified in the killing of a Mount Rainier National Park ranger today is believed to have military experience and survivalist skills .. and may have been involved in a shooting earlier in the day that left four wounded in Skyway .. W...,abcnews.go.com,link,shared_story,101,52,57,0,0,0,0,0,0,http://abcnews.go.com/blogs/headlines/2012/01/person-of-interest-identified-in-park-ranger-killing/,https://external.xx.fbcdn.net/safe_image.php?d=AQCZpWo4bhRDh6u7&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FUS%2Fap_rainier_120101_wblog.jpg&cfs=1&l,2/1/12 3:54
abc-news,86680728811_10150502770493812,86680728811,Air Travel Predictions for 2012,What are your predictions for the year ahead?,2012 will be full of ups and downs for air travelers.,abcnews.go.com,link,published_story,118,100,0,0,0,0,0,0,0,http://abcnews.go.com/Travel/BusinessTravel/top-air-travel-predictions-2012/story?id=15234459#.Tvms03KwXHo,https://external.xx.fbcdn.net/safe_image.php?d=AQDWH2cwIMl0R__d&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FTravel%2Fgty_air_travel_jef_111226_me.jpg&cfs=1,2/1/12 14:00
abc-news,86680728811_10150503135498812,86680728811,Generation X Turns 40 - Will the Slackers Get the Last Laugh?,Turning 40 are the creators of Borat .. South Park and even the first electric car.,Gen Xers are accomplishing stuff .. yet America couldn_t care less. So what will be their legacy?,abcnews.go.com,link,published_story,134,51,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/headlines/2011/12/genration-x-turns-40-will-the-slackers-get-the-last-laugh/,https://external.xx.fbcdn.net/safe_image.php?d=AQD7C8kb0Cybskct&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FUS%2Fgty_jon_hamm_thg_111229_main.jpg&cfs=1,2/1/12 17:00
abc-news,86680728811_10150503526528812,86680728811,Obamas Record-High Deportations Draw Hispanic Scorn,Fifty-nine percent of Latinos said they disapprove of the president_s approach to removing illegal immigrants.,The record-setting deportation of illegal immigrants under President Obama has drawn scorn from Hispanic Americans .. despite recent administration...,abcnews.go.com,link,published_story,152,398,0,0,0,0,0,0,0,http://abcnews.go.com/blogs/politics/2011/12/obamas-record-high-deportations-draw-hispanic-scorn/,https://external.xx.fbcdn.net/safe_image.php?d=AQBWjD6ntlyRLfhu&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fblogs%2Fpolitics%2F2011%2F12%2Fobamas-record-high-deportations-draw-hispanic-scorn%2Fno_facebook_preview_picture.jpg&cfs=1,2/1/12 20:00
abc-news,86680728811_208984995857060,86680728811,LA Arson Suspect Motivated by Deportation?,A prime suspect arrested in a series of 53 blazes in the Los Angeles area told authorities upon his detention .. I hate America .. according to ABC News sources directly involved in the case.,Police release surveillance video .. have person of interest in arson fires.,abcnews.go.com,link,shared_story,136,110,68,0,0,0,0,0,0,http://abcnews.go.com/US/la-fire-suspect-involved-deportation-dispute-sources/story?id=15272836,https://external.xx.fbcdn.net/safe_image.php?d=AQAGvzzG_AdbxoBD&w=130&h=130&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FUS%2Fap_la_arson_120101_me.jpg&cfs=1,2/1/12 22:02
abc-news,86680728811_293241904056065,86680728811,Child Obesity Ads: Public Service or Put-Down?,Does this anti-obesity campaign go too far?,Critics say Georgias obesity campaign sparks fear .. not motivation.,abcnews.go.com,link,shared_story,153,388,105,0,0,0,0,0,0,http://abcnews.go.com/Health/Wellness/stop-sugarcoating-child-obesity-ads-draw-controversy/story?id=15273638,https://external.xx.fbcdn.net/safe_image.php?d=AQA5woDDZ6FeeRaZ&w=116&h=116&url=http%3A%2F%2Fa.abcnews.go.com%2Fimages%2FHealth%2Fht_childhood_obesity_ads_tk_120102_me.jpg&cfs=1&sx=19&sy=0&sw=116&sh=116,2/1/12 22:05

I have:

awk -F 'Barack Obama {sum+=$13} END {print sum}' FB_Dataset.csv

However, it doesnt work. I'm new to unix could anyone solve this? Thank you.

PS: Its a csv file with comma delimiter.

1

awk uses the -F to specify the delimiter that will be used. In your case you are setting your delimiter to:

'Barack Obama {sum+=$13} END {print sum}' 

which is invalid.

Since you are working with a csv you will want the delimiter to be a comma, like so:

awk -F ',' ...

Additionally to perform a regex search in awk you use the /pattern/ syntax, like so:

awk -F ',' '/Barack Obama/ { ... }' FB_Dataset.csv

In this case, any line that matches the regex pattern Barack Obama will have the { ... } action taken against it.

You want that action to be that the 13th column is added to sum so:

awk -F ',' '/Barack Obama/ { sum += $13 }' FB_Dataset.csv

However this won't actually print anything, we can use the END statement to specify an action that should be taken after awk is finished processing all input:

awk -F ',' '/Barack Obama/ { sum += $13 } END { print sum }' FB_Dataset.csv

Note: Column 13 contains all 0's in your input so this will produce 0.

  • @Brandon: Does my update help? – jesse_b Oct 25 '19 at 17:59
  • Yes absolutely. Youre my life saviour!! THANK YOU. How can I give you reputation or something? – Brandon Oct 25 '19 at 18:01
  • Hi, one more question. I now need to select the posts where “Trump” (Ignore the case) is mentioned in the post content and number of likes (column 10) for those posts are greater than 100. And generate a new file with post_id and sorted like_count and name it “trump.txt”. I've tried awk -F ',' '/[tT]rump/ $10>100 {print $2, $10}' FB_Dataset.csv > trump.txt but it seems to generate only the second column with no header or 10th column. – Brandon Oct 25 '19 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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