{"id":2298,"date":"2017-05-08T10:06:03","date_gmt":"2017-05-08T10:06:03","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=2298"},"modified":"2024-02-08T14:25:05","modified_gmt":"2024-02-08T14:25:05","slug":"counting-lines-words-using-go","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/counting-lines-words-using-go\/","title":{"rendered":"Counting lines and words using Go"},"content":{"rendered":"<p class=\"p6\">For those who need to count words and lines in text files, an easy approach for this matter is to use <a style=\"color: #f1b31e;\" href=\"https:\/\/golang.org\/pkg\/bufio\/#ScanWords\">bufio.ScanWords<\/a> and <a style=\"color: #f1b31e;\" href=\"https:\/\/golang.org\/pkg\/bufio\/#ScanLines\">bufio.ScanLine<\/a> in order to quickly solve the problem.<\/p>\n<p class=\"p6\"><span class=\"s1\">To count words:<\/span><\/p>\n<pre class=\"lang:go decode:true\" title=\" Count the words.\">input := \"Spicy jalapeno pastrami ut ham turducken.\\n Lorem sed ullamco, leberkas sint short loin strip steak ut shoulder shankle porchetta venison prosciutto turducken swine.\\n Deserunt kevin frankfurter tongue aliqua incididunt tri-tip shank nostrud.\\n\"\r\nscanner := bufio.NewScanner(strings.NewReader(input))\r\n\r\n\/\/ Set the split function for the scanning operation.\r\nscanner.Split(bufio.ScanWords)\r\n\r\n\/\/ Count the words.\r\ncount := 0\r\nfor scanner.Scan() {\r\n\u00a0 \u00a0 count++\r\n}\r\n\r\nif err := scanner.Err(); err != nil {\r\n\u00a0 \u00a0 fmt.Fprintln(os.Stderr, \"reading input:\", err)\r\n}\r\n\r\nfmt.Printf(\"%d\\n\", count)<\/pre>\n<p><a style=\"color: #f1b31e;\" href=\"https:\/\/golang.org\/pkg\/bufio\/#ScanWords\">ScanWords<\/a> is a split function for a Scanner that returns each space-separated (checks <a style=\"color: #f1b31e;\" href=\"https:\/\/golang.org\/pkg\/unicode\/#IsSpace\">unicode.IsSpace<\/a>) word of text, with trimmed whitespace.<\/p>\n<p class=\"p6\"><span class=\"s1\">To count lines:<\/span><\/p>\n<pre class=\"lang:go decode:true\" title=\"Count the lines\">input := \"Spicy jalapeno pastrami ut ham turducken.\\n Lorem sed ullamco, leberkas sint short loin strip steak ut shoulder shankle porchetta venison prosciutto turducken swine.\\n Deserunt kevin frankfurter tongue aliqua incididunt tri-tip shank nostrud.\\n\"\r\nscanner := bufio.NewScanner(strings.NewReader(input))\r\n\r\n\/\/ Set the split function for the scanning operation.\r\nscanner.Split(bufio. ScanLines)\r\n\r\n\/\/ Count the lines.\r\ncount := 0\r\nfor scanner.Scan() {\r\n\u00a0 \u00a0 count++\r\n}\r\n\r\nif err := scanner.Err(); err != nil {\r\n\u00a0 \u00a0 fmt.Fprintln(os.Stderr, \"reading input:\", err)\r\n}\r\n\r\nfmt.Printf(\"%d\\n\", count)<\/pre>\n<p><a style=\"color: #f1b31e;\" href=\"https:\/\/golang.org\/pkg\/bufio\/#ScanLines\">ScanLines<\/a> is a split function for a Scanner that returns each line of text (separated by &#8220;\\r?\\n&#8221;). It returns also empty lines\u00a0and the last line is\u00a0returned even if it has no newline at the end.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those who need to count words and lines in text files, an easy approach for this matter is to [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":3286,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,86],"tags":[100,112,113,146,147,219,220,221],"yst_prominent_words":[3,2386,2410,2509],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2298"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=2298"}],"version-history":[{"count":1,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2298\/revisions"}],"predecessor-version":[{"id":132540,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2298\/revisions\/132540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/3286"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=2298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=2298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=2298"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}