LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sed in bash to remove whitespace (https://www.linuxquestions.org/questions/programming-9/using-sed-in-bash-to-remove-whitespace-139551/)

jimieee 01-28-2004 07:21 AM

Using sed in bash to remove whitespace
 
Hi, I'm new to sed and need to be able to use it in a bash script to remove whitespace from some html code. I've got a variable called regionWholeLine in my script:

(because the forum automatically deletes them I have to represent whitepace with ....)

.................................................<strong>Asia-Pacific</strong><br />

so I used the following code:

regionWholeLine="`echo "$regionWholeLine" | sed -e :a -e 's/^[ .............. ]*<strong/<strong /'`"

However, it doesn't seem to work and I've no idea why, because elsewhere in my script I use similar code to do exactly the same thing:

titleWholeLine="`echo "$titleWholeLine" | sed -e :a -e 's/^[ ......... ]*<td class/<td class/'`"

works on:

.......................................................<td class="title">

Any ideas why I'm having problems?

Thanks,

~James~

jim mcnamara 01-28-2004 09:57 AM

If you use code tags
[ c o d e ]
.........
[ / c o d e ] (without the spaces ) the forum won't chop off leading blanks.

Here is a start:
Code:

tmp=`echo "$regionWholeLine" | sed 's/  / /g' | sed 's/^[ ]//' `
echo $tmp


crabboy 01-28-2004 10:01 AM

How about this:

test.txt
Code:

   
    <hello>
      <dude>

Code:

# cat test.txt | sed 's/^[ ]*//'
<hello>
<dude>
#


jimieee 01-28-2004 10:33 AM

Ah! I figured it out! Thanks for your replies, it helped me understand a little better about how sed works (I could only find very complicated documentation). It was all because I was using a space, instead of a tab inbetween the [ ] :)

Cheers,

~James~


All times are GMT -5. The time now is 09:24 AM.