Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Consecutive Values Multiple Times

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
Say I have a variable "x" defined as ....

x = "abc 123 xyz 456"
Note: the # of spaces in each grouping is : 3 2 4 respectively and will always vary in # of spaces.

I want to replace each group of spaces with some value, say "P". Anotherwords, I want to treat each space grouping individually and replace with a single value irregardless of the number of spaces in each grouping.

For example, my intended result should be:

newx = "abcP123PxyzP456"

The following doesn't get what I want:

newx='echo "abc 123 xyz 456" | /usr/xpg4/bin/sed s/"([[:space:]]\)"/"P"/g'; echo $newx

Resulting newx: abcPPP123PPxyzPPPP456

Notes:
The reason I'm using the [[:space:]] class of characters is because the spaces could potentially be tabs (or multiple # of tabs).
I tried using the the folowing:
newx='echo "abc 123 xyz 456" | /usr/xpg4/bin/sed s/"([[:space:]]\)."/"P"/g'; echo $newx

Resulting newx: abcPP123PxyzPP456

Close, but not quite there ... any suggestions?
 
[tt]sed 's/ */P/g'[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
that doesn't work (I get "P" replacements all over the place) ...

PaPbPcPP1P2P3PPxPyPzPP4P5P6P
 
Works for me, I type:
echo "abc 123 xyz 456" | sed 's! *!P!g'
Result is:
abcP123PxyzP456

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
tr -s ' '|tr ' ' 'P'

first tr squeezes all consecutive spaces into 1 space

second tr converts space chars to 'P' chars



HTH,

p5wizard
 
p5wizard,
I'm not sure why your doing nested "tr"'s (plus having a little trouble deciphering your syntax ...could u repost for me?), but I did get it to work somewhat using a single tr and using [:space:] option. BUUUUTTTTTT ... I can't seem to get it to work if I have tabs as spaces....
 
And this ?
sed 's![[:space:]][[:space:]]*!P!g'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,
echo "abc 123 456" | sed '[[:space:]][[:space:]]*/P/g'

I get no substitutions !!!!

Why are you using two of the :space: char. sets? One should suffice, should it?

...and yes I'm using the POSIX version of sed ...

....this is bugging the #$%*& out of me !!!
 
I meant to post that:
echo "abc 123 456" | sed 's/[:space:][:space:]*/P/g'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
woops, sorry, I wasn't at one point using POSIX (flipping around too much) ... IT WORKS NOW!!!

...but why the double :space: char sets?
 
Perhaps with posix sed this will suffice:
echo "abc 123 456" | sed 's/[:space:]+/P/g'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Nahhh,
thanks, but the [:space:]+ didn't work ... but the double one does .... curious, why though??

Now if I can only recall why I needed to do this in the 1st place???
Time to go home ...
 
And this ?
echo "abc 123 456" | sed 's/[:space:]\{1,\}/P/g'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
some tr implementations do not squeeze (-s) and translate (str1 and str2) in one go. On AIX you can combine both operations

and it's not nested, it is just two tr's piped together. As for deciphering my syntax, look at the lines below the commandline explaining the two tr's in my previous post...

And I forgot about the tabs (my mistake).

so:

Code:
tr '[:space:]' ' '          |      tr -s ' ' 'P'
#convert any whitespace    pipe    squeeze spaces into
#to space 0x20                     one space, then convert
#                                  that space to 'P'

or if you cannot squeeze and convert in one go, then another pipe and tr would be necessary. Perhaps the sed solutions provided by others are better...

HTH,

p5wizard
 
My final findings ...

Thanks p5wizard & PHV for all your help ...

The following 3 methods work:
METHOD A:
tr -s '[:space:]' ' ' | tr -s ' ' 'P'

...and yes, for Solaris 9, I need to pipe tr twice (doesn't work the way I want it if I attempt to do it in 1 go).

METHOD B (two variations):

sed 's/[[:space:]][[:space:]]*/P/g'

works, but I still don't understand why two of the :space: char. sets are needed?

sed 's/[[:space:]]\{1,\}/P/g'

works great too!

METHOD C:

awk '{gsub(/[[:space:]]+/,"P"); print}'








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top