使用 awk 根据其他列的值对列的值求和

使用 awk 根据其他列的值对列的值求和

我正在尝试使用 awk 对列中的某些数字求和。我想对“Barack Obama”的第 13 列(love_count)进行求和以获得总数。每当行中包含“Barack Obama”一词时,我就需要对 love_count 进行求和。这是输入文件的示例:

    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

我有:

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

然而,它不起作用。我是unix新手,谁能解决这个问题?谢谢。

PS:它是一个带有逗号分隔符的csv文件。

答案1

awk使用-F来指定将使用的分隔符。在您的情况下,您将分隔符设置为:

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

这是无效的。

由于您正在使用 csv,因此您将希望分隔符为逗号,如下所示:

awk -F ',' ...

awk此外,要在您使用的语法中执行正则表达式搜索/pattern/,如下所示:

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

在这种情况下,任何与正则表达式模式匹配的行都Barack Obama{ ... }对其采取操作。

您希望该操作是将第 13 列添加到总和中,如下所示:

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

然而,这实际上不会打印任何内容,我们可以使用该END语句来指定应该采取的操作 awk处理完所有输入:

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

注意:第 13 列包含输入中的全 0,因此将生成0.

相关内容