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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-13-2017   -   Original Discussion by dineshaila
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 11:37 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 05-13-2017   -   Original Discussion by dineshaila
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 19 November 2017, 1:43 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 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   -   Original Discussion by dineshaila
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 November 2017, 12:12 PM EST
Location: Aachen, Germany
Posts: 11,622
Thanks: 320
Thanked 3,616 Times in 3,321 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   -   Original Discussion by dineshaila
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   -   Original Discussion by dineshaila
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 19 November 2017, 1:43 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 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   -   Original Discussion by dineshaila
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 November 2017, 6:17 PM EST
Location: Houston, Texas, USA
Posts: 983
Thanks: 0
Thanked 338 Times in 319 Posts

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

Sponsored Links
Closed

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 02:53 PM
How to create a print filter that print text & image? chedlee88-1 UNIX for Dummies Questions & Answers 1 11-19-2013 11: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 04:24 PM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 02:00 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 10:35 AM



All times are GMT -4. The time now is 04:40 AM.