[SOLVED] Bash - Replace a text with a variable containing new lines character
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.
Bash - Replace a text with a variable containing new lines character
Hey All,
I am trying to replace a variable in sed, usually, it can be done by double quotes but my variable has new lines characters and backslashes which somehow is not working.
The two character text string "\n" is just that - not a newline unless interpolated somehow. Perl, sed would make no difference.
Embedding 0x0A might work. Untested.
#1 this appears to be a SED question, not a BASH question, so your title is misleading.
#2 for what you want to do, I am not sure that SED is the correct tool. PERL would be optimal, GSAR would serve.
Could we have some detail about what you are really trying to achieve, and why you selected SED (not BASH) to approach the problem?
Hey wpeckham,
I deliberately chose bash and not sed, as I am on bash shell and (as you correctly hinted) that sed may not be the correct tool. I have tried sed, because that was the first tool which came into my mind for replacing text and which has been working great for all my needs so far.
I think the two characters \ and n should be kept here. But a RE interprets it.
How about bash (as the title already suggests)?
Code:
str="--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--"
while IFS= read -r line
do
case $line in
(CLIENT_KEY*) line=$str;;
esac
printf "%s\n" "$line"
done <myfile
Last edited by MadeInGermany; 05-12-2018 at 03:25 AM.
Reason: Added missing print
To prevent things like slash codes from being interpreted by the shell/command line and programs you would use single quotes instead of doble quotes. Somehow I doubt if that is really what you want. Normally a key file will contain embedded newlines at those locations. Why does the interpretation of the newline concern you?
I think the two characters \ and n should be kept here. But a RE interprets it.
How about bash (as the title already suggests)?
Code:
str="--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--"
while IFS= read -r line
do
case $line in
(CLIENT_KEY*) line=$str;;
esac
printf "%s\n" "$line"
done <myfile
Excellent, this solves my problem. Thanks much MadeInGermany.
To prevent things like slash codes from being interpreted by the shell/command line and programs you would use single quotes instead of doble quotes...
You mean
Code:
str='...'
Agreed.
Indeed within " " the shell does special treatment of $ and " and ` characters (and allows \ escape of them, therefore the \ character is a bit special, too).
BTW once stored in a variable, the shell does not do further special treatment of "$str"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.