Akdora’s Blog

Programming, Oracle, Life, Fun

How to remove spaces in a string in Shell March 8, 2011

Filed under: Shell — Akdora @ 9:45 am
Tags: , , ,

I experienced that when you connect to Oracle database from shell and get some data from the table, if the length of the column value is more than 80 letters. Shell puts space in every 80th letter automaticly. I do not know why, but it is really interesting. I counted the number 80 for my environment. It may differ in different systems.

Anyway, I need to remove that interesting spaces. So here is a script to do this.

RETVAL=`echo $RETVAL | sed 's/ //g'`

We use the SED command. Its use like ‘s/[seach_letter]/[replacement]/g’. In my example [seach_letter] is ” ” space and [replacement] is nothing. For example; If we want to replace ”  ” (space) characters with “_” (underscore) character. Here is how we do this:


RETVAL=`echo $RETVAL | sed 's/ /_/g'`

I wrote this quick entry, because if you do not remember how to do something like this and want to google it. It is not easy to find sometimes 🙂