Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-28-2004, 07:21 AM   #1
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Rep: Reputation: 15
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?


Old 01-28-2004, 09:57 AM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 35
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:
tmp=`echo "$regionWholeLine" | sed 's/  / /g' | sed 's/^[ ]//' `
echo $tmp
Old 01-28-2004, 10:01 AM   #3
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
How about this:

# cat test.txt | sed 's/^[ ]*//'
Old 01-28-2004, 10:33 AM   #4
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
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 [ ]




Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to remove word wrap Harpune Linux - Software 2 03-02-2009 06:53 PM
problem creating BASH script with files that have whitespace scattaneo Linux - General 2 11-06-2004 05:40 PM
remove whitespace at end of file FunkyRes Programming 2 10-05-2004 12:31 AM
bash - sed/tr??? pk21 Programming 2 09-05-2003 07:56 AM
bash - sed pk21 Programming 6 03-07-2003 11:02 AM

All times are GMT -5. The time now is 03:33 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration