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 11-28-2005, 09:51 AM   #1
revof11
Member
 
Registered: Nov 2003
Location: Macungie, PA
Distribution: Fedora Core 9, Gentoo 2008.0
Posts: 108

Rep: Reputation: 15
Shell Script - String Replacement


I have a shell script that gathers data from a command that is run.
The output is stored in a variable (let us call it .xxx).

I want to replace all blank spaces in the variable with a newline so I can run an echo -e on the data. I attempted to use the following code (along with a set of variations including ' ' and " "), but with no luck:
Code:
parsedList=${tempString// /'\n'}
Any suggestions?

I threw together a quick sample here of what I'm doing:
Code:
qaResult=$(rpm -qa $1)
qaParsed=${qaResult// /'\n'}
echo -e $qaParsed
If I put any known pattern maced between // and / it works just fine.
Thanks for your time, everyone!
 
Old 11-28-2005, 10:31 AM   #2
revof11
Member
 
Registered: Nov 2003
Location: Macungie, PA
Distribution: Fedora Core 9, Gentoo 2008.0
Posts: 108

Original Poster
Rep: Reputation: 15
Lightbulb

This is a little more messy, but it works for my needs.
Using the example that I listed before... here is the work-around I came up with:

Code:
qaResult=$(rpm -qa $1)
qaParsed=`echo "$qaResult" | tr -c '\012[a-zA-Z][0-9].\-_' '\n' | uniq`
echo -e "$qaParsed"
If anyone comes up with a solution for using ${<expr>//<search>/<repl>}, please post.
Thanks!

 
Old 11-28-2005, 10:32 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
for word in $XXX; do
echo $word
done;
 
Old 11-28-2005, 10:35 AM   #4
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Code:
~> cat x
sa
dasdasd asdf     asd ass ss
~> cat x | sed "s/ /\n/g" | sed "/^$/d"
sa
dasdasd
asdf
asd
ass
ss
Is this what you wanted?
 
Old 11-28-2005, 01:30 PM   #5
revof11
Member
 
Registered: Nov 2003
Location: Macungie, PA
Distribution: Fedora Core 9, Gentoo 2008.0
Posts: 108

Original Poster
Rep: Reputation: 15
I'm going to go with bigearsbilly response.
I honestly didn't even think of trying that.

Funny how you overlook the obvious solution sometimes.
 
Old 11-28-2005, 06:04 PM   #6
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Quote:
Originally posted by bigearsbilly
for word in $XXX; do
echo $word
done;
Quote:
Originally posted by revof11
Funny how you overlook the obvious solution sometimes.
I admit that's true.
 
Old 11-29-2005, 04:09 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
that's what 's so fab about unix/linux.
always learning
 
Old 11-29-2005, 06:38 AM   #8
revof11
Member
 
Registered: Nov 2003
Location: Macungie, PA
Distribution: Fedora Core 9, Gentoo 2008.0
Posts: 108

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by bigearsbilly
that's what 's so fab about unix/linux.
always learning
... and the community.
In the ???? years that I've been using Linux (it's getting pretty long now) the community has never let me down.

Rock on.
 
  


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
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 08:47 PM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
Get video DVD title string for shell script? cheesekeeper Linux - Hardware 9 05-05-2005 05:53 AM
exploding string into individual characters using a shell script farmerjoe Programming 9 10-13-2004 03:23 AM


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

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