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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print & and \n while replacing with sed/awk?
# 1  
Old 05-13-2017
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-13-2017 at 11:37 AM.. Reason: Added CODE tags.
# 2  
Old 05-13-2017
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
# 3  
Old 05-13-2017
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 05-13-2017
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
# 5  
Old 05-13-2017
Quote:
Originally Posted by dineshaila
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
# 6  
Old 05-15-2017
Code:
string='din\&esh\\nisgood'
sed "s#[[]Name[]]#${string}#g" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

3. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

awk Help: Filter Multiple Entry & print in one line.

AWK Gurus, data: srvhcm01 AZSCI srvhcm01 AZSDB srvhcm01 BZSDB srvhcm01 E2QDI31 srvhcm01 YPDCI srvhcm01 YPDDB srvhcm01 UV2FSCR srvhcm01 UV2FSBI srvhcm01 UV2FSXI srvhcm01 UV2FSUC srvhcm01 UV2FSEP srvhcm01 UV2FSRE srvhcm01 NASCI srvhcm01 NASDB srvhcm01 UV2FSSL srvhcm01 UV2FSDI (7 Replies)
Discussion started by: rveri
7 Replies

5. Shell Programming and Scripting

Replacing FQDN by hostnames in a CSV file with sed & awk

Hello, Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file: Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==... (2 Replies)
Discussion started by: Wonto
2 Replies

6. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

7. Shell Programming and Scripting

AWK - Pattern Matching & Replacing - Performance

Experts, I am a beginner to Unix Shell Scripting We have source as a flat file which contains CTRL+F character as the delimiter. We need to count the number of records in the file (CTRL+F) to perform file validation Following command being used: awk '{cnt+=gsub(//,"&")}END {print cnt}'... (4 Replies)
Discussion started by: srivijay81
4 Replies

8. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

9. Shell Programming and Scripting

awk pipes & print

Hi, I need to awk some data from some text files, basic stuff, eg: awk '/phrase/ {print $1,$2,$3}' file Which will print columns 1 to 3 of all lines containing "phrase" But what if I wanted to pipe just one of the columns to a command (in my case a date converter) as in: awk... (4 Replies)
Discussion started by: Jonny2Vests
4 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question