Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to print & and \n while replacing with sed/awk?

Shell Programming and Scripting


Tags
sed - replace text with variable, sed awk, sed awk grep replace newline

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-13-2017
dineshaila dineshaila is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 8 June 2017, 3:34 AM EDT
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
How to print & and \n while replacing with sed/awk?


Code:
string="din&esh\nisgood"

File.txt:

Code:
the name is [Name]


Code:
sed "s#\[Name\]#${string}#g" File.txt

Output am getting:

Code:
the name is din[Name]esh
is good

Expected output:

Code:
the name is din&esh\nisgood

The input string is dynamic it will be keep on changing

am able to handle & by placing \& in the string..




Moderator's Comments:
How to print & and \n while replacing with sed/awk? Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-13-2017 at 10:37 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 05-13-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 24 September 2017, 2:31 AM EDT
Location: Chennai
Posts: 2,608
Thanks: 571
Thanked 1,238 Times in 1,116 Posts
Hello dineshaila,

Could you please try following and let me know if this helps you.

Code:
string="din&esh\nisgood"
awk -vString=$string '{sub(/\n/,"",String);sub(/&/,"\\&",String);sub(/\[Name\]/,String);print}' Input_file

Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 05-13-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 September 2017, 9:57 AM EDT
Location: Aachen, Germany
Posts: 11,370
Thanks: 298
Thanked 3,524 Times in 3,242 Posts
1) excape the & with a backslash, and escape the \n's backslash as well
2) use single quote to prevent expansion by the shell

Code:
string='din\&esh\\nisgood'
sed "s#\[Name\]#${string}#g" file
the name is din&esh\nisgood

    #4  
Old Unix and Linux 05-13-2017
dineshaila dineshaila is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 8 June 2017, 3:34 AM EDT
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
I have tried escaping the & and \n
the output is
the name is din&esh
isgood

---------- Post updated at 11:14 AM ---------- Previous update was at 11:12 AM ----------

Hi Ravinder,
u r replacing the \n with blank.. but i need \n in the output



Also my input string will be dynamically changing
it may or may not have \n and &
it may have other regex
My output should display all the characters in string. it should be replaced with regex
Sponsored Links
    #5  
Old Unix and Linux 05-13-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 24 September 2017, 2:31 AM EDT
Location: Chennai
Posts: 2,608
Thanks: 571
Thanked 1,238 Times in 1,116 Posts
Quote:
Originally Posted by dineshaila View Post
Hi Ravinder,
u r replacing the \n with blank.. but i need \n in the output
Also my input string will be dynamically changing
it may or may not have \n and &
it may have other regex
My output should display all the characters in string. it should be replaced with regex
Hello dineshaila,

Solutions always being provided on the sample Input_file shown and conditions provided, in your first post it was no where mentioned that there could be permutations and combinations. Kindly mention all the conditions of your requirement and with very near sample Input_file so that we could try to help you in spite of guessing each time.

Thanks,
R. Singh
Sponsored Links
    #6  
Old Unix and Linux 05-15-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 22 September 2017, 8:32 AM EDT
Location: Houston, Texas, USA
Posts: 970
Thanks: 0
Thanked 330 Times in 312 Posts

Code:
string='din\&esh\\nisgood'
sed "s#[[]Name[]]#${string}#g" infile

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing echo with print demmel Shell Programming and Scripting 7 04-16-2015 01:53 PM
How to create a print filter that print text & image? chedlee88-1 UNIX for Dummies Questions & Answers 1 11-19-2013 10:40 AM
print first few lines, then apply regex on a specific column to print results. kchinnam Shell Programming and Scripting 4 08-24-2010 03:24 PM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 01:00 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 09:35 AM



All times are GMT -4. The time now is 02:36 AM.