Latest LQ Deal: Linux Power User Bundle
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 12-12-2005, 03:02 PM   #1
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Whitespace parsing sed?

I am writing a shell script to pull apart words from text and process each word. I originally set out by having something like:

TEXT="This is some text.  It has words, numbers (123.4), punctuation (!@#$$%), just stuff that people normally type."
for WORD in $TEXT; do
   #Process WORD
   #Append results to buffer.
I noticed, though, I was only adding a single space between words when I was reassembling the output, and I really want to preserve the original whitespace.

My new thought was to have a loop like this:
1) Get any leading space.
2) Get any nonspace characters up to but not including a space.
3) result = Process(non-space)
4) append space+result to buffer
5) repeat.

I am pretty sure I want to use sed to do parts 1 & 2, but I am having trouble getting myself in the proper mindset-- I can figure out how to replace pieces of the pattern space, but I am a little iffy on pulling out just the matches I want without the rest of $TEXT.

Does anybody have any pointers?
Old 12-12-2005, 05:24 PM   #2
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I'd probably process character by character. Save off non-whitespace characters to a buffer. When you hit a whitepsace char process the character buffer and append to results. Then copy the whitespace character(s) one for one to the results buffer. When you hit the next non-whitespace char repeat above.


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
Handling whitespace in For loop rvoigt Linux - General 1 04-06-2005 07:57 AM
sed parsing question ncblues Linux - Newbie 5 01-03-2005 07:36 AM
remove whitespace at end of file FunkyRes Programming 2 10-05-2004 01:31 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 11:33 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 PM.

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