Bash: How to trim leading, following spaces from text

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 as well as convert multiple spaces to a single space inside string:
awk '{$1=$1;print}'
or shorter:
awk '{$1=$1};1'

Do you have such handy commands / snippets you would like to share? Please share in the comments below.

Author: Angsuman Chakraborty

Software architect and entrepreneur with substantial experience in designing and developing enterprise software and services for Fortune 500 companies as well as startups. Enjoys solving complex software and bioinformatics problems. Currently heading a product development & outsourcing company specializing in Web 2.0 technologies. Specialties: Software Architecture, Software Design, Software Consultant, Java Developer, Web 2.0, Custom product development, Bioinformatics, Software Outsourcing, Management Consultant

Leave a Reply

Your email address will not be published. Required fields are marked *