{"id":15556,"date":"2018-08-02T12:50:52","date_gmt":"2018-08-02T07:20:52","guid":{"rendered":"http:\/\/blog.taragana.com\/?p=15556"},"modified":"2018-08-02T12:50:52","modified_gmt":"2018-08-02T07:20:52","slug":"bash-how-to-trim-leading-following-spaces-from-text","status":"publish","type":"post","link":"https:\/\/blog.taragana.com\/bash-how-to-trim-leading-following-spaces-from-text-15556","title":{"rendered":"Bash: How to trim leading, following spaces from text"},"content":{"rendered":"
Many Linux\/Unix commands are shown formatted like Many Linux\/Unix commands are shown formatted like ps -ef for instance. As such it becomes difficult to process them subsequently in the pipeline, like for instance passing it to xargs kill for killing the filtered errant processes. The command below, when part of a bash \/ sh pipeline will remove the leading and following spaces […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/posts\/15556"}],"collection":[{"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/comments?post=15556"}],"version-history":[{"count":0,"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/posts\/15556\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/media?parent=15556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/categories?post=15556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.taragana.com\/wp-json\/wp\/v2\/tags?post=15556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}ps -ef<\/code> for instance. As such it becomes difficult to process them subsequently in the pipeline, like for instance passing it to
xargs kill<\/code> for killing the filtered errant processes. The command below, when part of a bash \/ sh pipeline will remove the leading and following spaces as well as convert multiple spaces to a single space inside string:
\nawk '{$1=$1;print}'<\/code>
\nor shorter:
\nawk '{$1=$1};1'<\/code>
\nDo you have such handy commands \/ snippets you would like to share? Please share in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"