LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-11-2018, 06:04 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question 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.

This is a sample variable
Code:
# echo $CLIENT_KEY
--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n--END PRIVATE KEY--
#
I want to replace this variable's value in a file
Code:
# cat myfile
ABC
CLIENT_KEY
#

# cat myfile | sed "s/CLIENT_KEY/${CLIENT_KEY}/"  (does not works)
ABC
--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n--END PRIVATE KEY--
#
Thanks!

Last edited by vikas027; 05-11-2018 at 08:12 AM. Reason: Simplifying the variables
 
Old 05-11-2018, 06:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I'm not sure how it can be done with the multiple line replacement. It's easy with Perl 5 however.

Code:
cat myfile | perl -p -e "s|CLIENT_KEY|${CLIENT_KEY}|"
 
Old 05-11-2018, 06:23 AM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
#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?
 
Old 05-11-2018, 07:01 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 05-11-2018, 07:32 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Help us to help you. Start by constructing a simplified InFile such as ...
Code:
--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--
... and then construct the desired OutFile. Post both files here.

These files will facilitate understanding and give us an example for testing.

Daniel B. Martin

.
 
Old 05-11-2018, 08:16 AM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Turbocapitalist View Post
I'm not sure how it can be done with the multiple line replacement. It's easy with Perl 5 however.

Code:
cat myfile | perl -p -e "s|CLIENT_KEY|${CLIENT_KEY}|"
Hey,

Thanks, but this did not work, it interpolates "\n" into new lines. I've tried with perl v5.16.3
 
Old 05-11-2018, 08:19 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by wpeckham View Post
#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.

Last edited by vikas027; 05-11-2018 at 08:46 AM.
 
Old 05-11-2018, 08:59 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
ABC
CLIENT_KEY
DEF
... this code ...
Code:
str="--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--"
sed "s/CLIENT_KEY/$str/"<$InFile >$OutFile
echo "InFile ...";  cat $InFile;  echo "End Of InFile ($(wc -l <$InFile) lines)."
echo
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)."
... produced this result ...
Code:
InFile ...
ABC
CLIENT_KEY
DEF
End Of InFile (3 lines).

OutFile ...
ABC
--BEGIN PRIVATE KEY--
AAA
BBB
CCC

--END PRIVATE KEY--
DEF
End Of OutFile (8 lines).
Daniel B. Martin

.
 
Old 05-11-2018, 09:10 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by vikas027 View Post
... it interpolates "\n" into new lines. I've tried with perl v5.16.3
That's what you asked for above. If not please show the files as danielbmartin requested above so that we can see what you are trying to achieve.
 
Old 05-11-2018, 09:20 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
... Or maybe this is what you want ...

With this InFile ...
Code:
ABC
CLIENT_KEY
DEF
... this code ...
Code:
str="--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--"
sed "s/CLIENT_KEY/$str/"<$InFile   \
|paste -s -d~ -                    \
|sed 's/~/\\n/g'                   \
>$OutFile
echo "InFile ...";  cat $InFile;  echo "End Of InFile ($(wc -l <$InFile) lines)."
echo
echo "OutFile ..."; cat $OutFile; echo "End Of OutFile ($(wc -l <$OutFile) lines)."
... produced this OutFile ...
Code:
ABC\n--BEGIN PRIVATE KEY--\nAAA\nBBB\nCCC\n\n--END PRIVATE KEY--\nDEF
Daniel B. Martin

.

Last edited by danielbmartin; 05-11-2018 at 01:21 PM.
 
Old 05-12-2018, 03:22 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
2 members found this post helpful.
Old 05-12-2018, 06:45 AM   #12
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
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?
 
Old 05-12-2018, 08:25 AM   #13
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by MadeInGermany View Post
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.


Thanks everyone for your time.

Last edited by vikas027; 05-12-2018 at 08:29 AM.
 
Old 05-12-2018, 05:19 PM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by wpeckham View Post
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"
 
Old 05-30-2018, 08:47 PM   #15
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Lightbulb

I have also found a neat python solution for the same

Code:
f = open(file, 'r')
print f.read().replace('\n', '\\n')[:-1]
 
  


Reply

Tags
sed bash



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
[SOLVED] Replace a variable text with * pedropt Programming 8 12-30-2017 09:23 AM
[SOLVED] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 04:54 PM
SED - How to replace a control character (NAK) from a text file paulr211 Linux - Software 3 10-04-2012 10:14 AM
bash text to variable accessing individual text lines patolfo Programming 11 05-11-2010 10:21 AM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration