[SOLVED] Replace a string of blanks with a "tailored" string of equal length
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Replace a string of blanks with a "tailored" string of equal length
It is desired to replace strings of 3 or more blanks with "tailored" strings of equal length, such that the replacement string has one leading blank, one trailing blank, and all the interior characters are "%".
InFile ...
Code:
cat dog
red blue green
apple pear
honda toyota nissan
Desired OutFile...
Code:
cat dog
red % blue green
% apple %%% pear
honda toyota %%%%%%% nissan
I hammered out a sed solution. It looks clumsy (3 steps) but works.
Step 1) Replace all blanks with %.
Step 2) Replace the leading character in a string of %s with a blank.
Step 3) Replace the trailing character in a string of %s with a blank.
With this InFile ...
Code:
cat dog
red blue green
apple pear
honda toyota nissan
... this sed ...
Code:
sed 's/ /%/g;s/\([^%]\)%%/\1 %/g;s/%\([^%]\)/ \1/g' $InFile >$OutFile
... produced this OutFile ...
Code:
cat dog
red % blue green
%% apple %%% pear
honda toyota %%%%%%% nissan
Isn't your OutFile missing a space at the beginning of the third line?
Yes. My proposed solution has a flaw.
Code:
sed 's/\B \B//g'
This plugs in no %s at all. Presumably you meant ...
Code:
sed 's/\B \B/%/g' $InFile >$OutFile
This sed is lovely!
Code:
sed 's/\B \B/%/g;s/^%/ /'
After making the original post I devised a debugging aid. It prints one line from InFile, the corresponding line from OutFile, and then a blank line, repeat, repeat.
Code:
paste -d "\n" $InFile $OutFile /dev/null
With this InFile ...
Code:
cat dog
red blue green
apple pear
honda toyota nissan
... this code ...
Code:
echo; echo "Method #2 of LQ Member millgates."
sed 's/\B \B/%/g;s/^%/ /' $InFile >$OutFile
# This PASTE interleaves three files.
# The third file (/dev/null) contains empty lines.
echo
paste -d "\n" $InFile $OutFile /dev/null
... produces this display ...
Code:
Method #2 of LQ Member millgates.
cat dog
cat dog
red blue green
red % blue green
apple pear
% apple %%% pear
honda toyota nissan
honda toyota %%%%%%% nissan
This didn't work until I remembered something from a recent unrelated post. Single percent signs sometimes vanish from posts. That explains why a sed solution posted by millgates seemed to fail, and also why your awk seemed to fail. Thanks to both of you for constructive and instructive solutions.
Daniel B. Martin
Last edited by danielbmartin; 10-16-2015 at 07:31 AM.
Reason: Cosmetic improvement
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.