{"id":1155,"date":"2014-12-03T11:44:43","date_gmt":"2014-12-03T10:44:43","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=1155"},"modified":"2014-12-03T15:19:12","modified_gmt":"2014-12-03T14:19:12","slug":"grep-result-to-a-shell-variable-no-line-breaks","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2014\/12\/grep-result-to-a-shell-variable-no-line-breaks\/","title":{"rendered":"Grep result to a shell variable &#8211; no line breaks"},"content":{"rendered":"<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-1157\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/bash-shell.png\" alt=\"bash-shell\" width=\"128\" height=\"128\" \/>There was this website containing regularly updated content I wanted to follow. Unfortunately, they had no RSS feed available and I didn&#8217;t feel like checking on the website every now and then. Also, what I needed was just the posts containing particular keywords in the Title. So I wrote a tiny script to do the mundane task for me and let me know when the keywords I was interested in would appear:<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\nTMPFILE=`mktemp \/tmp\/website_content.XXXXXX` || exit 1\r\n\r\nwget --output-document=\"$TMPFILE\" --user-agent=\"Mozilla\/5.0 (X11; Linux x86_64; rv:31.0) Gecko\/20100101 Firefox\/31.0 Iceweasel\/31.1.0\" http:\/\/someinterestingurl.com\/\r\n\r\nRESULT=`cat $TMPFILE | grep -i 'keyword-1\\|keyword-2\\|keyword-3'`\r\n\r\nif [[ `echo ${#RESULT}` -gt \"0\" && `echo $RESULT | wc -l` -ge \"1\" ]]; then\r\n\techo \"yes\"\r\n\techo $RESULT | mail -s \"new keywords found\" example@example.com\r\nfi\r\n\r\nrm $TMPFILE\r\n<\/pre>\n<p style=\"text-align: justify;\">However, grep would return only one line with all the results when assigning the result to the <em>$RESULT<\/em> variable, not line by line as it deos when printing to the standard output. The problem turned out to be bad usage of echo. <em>echo $RESULT<\/em> had to be replaced by <em>echo &#8220;${RESULT}<\/em>, my bad, after that the script worked just fine.<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\nTMPFILE=`mktemp \/tmp\/website_content.XXXXXX` || exit 1\r\n\r\nwget --output-document=\"$TMPFILE\" --user-agent=\"Mozilla\/5.0 (X11; Linux x86_64; rv:31.0) Gecko\/20100101 Firefox\/31.0 Iceweasel\/31.1.0\" http:\/\/someinterestingurl.com\/\r\n\r\nRESULT=`cat $TMPFILE | grep -i 'keyword-1\\|keyword-2\\|keyword-3'`\r\n\r\nif [[ `echo ${#RESULT}` -gt \"0\" && `echo \"${RESULT}\" | wc -l` -ge \"1\" ]]; then\r\n\techo \"yes\"\r\n\techo \"${RESULT}\" | mail -s \"new keywords found\" eaxmple@example.ccom\r\nfi\r\n\r\nrm $TMPFILE\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There was this website containing regularly updated content I wanted to follow. Unfortunately, they had no RSS feed available and I didn&#8217;t feel like checking on the website every now and then. Also, what I needed was just the posts &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2014\/12\/grep-result-to-a-shell-variable-no-line-breaks\/\"> <span class=\"screen-reader-text\">Grep result to a shell variable &#8211; no line breaks<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[18,41,42],"class_list":["post-1155","post","type-post","status-publish","format-standard","hentry","category-programming","tag-linux","tag-shell","tag-web"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=1155"}],"version-history":[{"count":8,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1155\/revisions"}],"predecessor-version":[{"id":1164,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1155\/revisions\/1164"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=1155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=1155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=1155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}