LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-12-2005, 02:02 PM   #1
carl.waldbieser
Member
 
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:

Code:
TEXT="This is some text.  It has words, numbers (123.4), punctuation (!@#$$%), just stuff that people normally type."
BUFFER=""
for WORD in $TEXT; do
   #Process WORD
   #Append results to buffer.
done
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?
Thanks!
 
Old 12-12-2005, 04:24 PM   #2
crabboy
Moderator
 
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.
 
  


Reply


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


All times are GMT -5. The time now is 11:10 PM.

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