Trimming White Space with Sed

You can use the following sed commands to trim white space from lines of text:

Trim white space from both sides of a string

  1. echo ' abc ' | sed 's/^ *//;s/ *$//'

Trim white space from the right of a string

  1. echo ' abc ' | sed 's/*//;s/ *$//'

Trim white space from the left of a string

  1. echo ' abc ' | sed 's/^ *//;s/$//'

Trim white space from both ends of every line in a file (keeping a .bak backup)

  1. sed -i.bak 's/^ *//;s/ *$//' file.txt

Trim white space from both ends of every line of every file in the current directory (keeping a .bak backup)

  1. ls -1 | while read line; do cat $line | sed -i.bak 's/^ *//;s/ *$//' $line

 

Bash script – read stdin line by line

This is a really useful bit of code which I use almost daily to speed up routine  tasks. You can pipe text, split into multiple lines, into a while loop and read through them line by line. This is how you do it:

Read a file line by line and echo (reversed) the line:

  1. cat file.txt | while read line
  2. do
  3. echo $line | rev
  4. done

Read the output of an ls and cat each file through sed:

  1. ls -1 | while read line
  2. do
  3. cat $line | sed 's/foo/bar/g'
  4. done

You’ll see that you ‘while read XXXX’. $XXXX then becomes the variable that references that line. So in the above example ‘while read line’ produces $line. Naturally, there are better ways to do the above tasks but it hopefully gives you an idea of the power of a while loop in bash.