sed replacing required newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replacing required newlines
# 1  
Old 07-31-2012
sed replacing required newlines

hi
i have a requirement to replace a string with another using sed and to get the result newline separated but after sed replacement the newline vanishes
below is sample code

Code:
 
#!/bin/ksh
set -x
string="name sam\nage 45 \nsport soccer"
echo $string
string=`echo $string | sed "s/soccer/tennis/g"`
echo $string

result
Code:
 
+ string=name sam\nage 45 \nsport soccer
+ echo name sam\nage 45 \nsport soccer
name sam
age 45
sport soccer
+ + echo name sam\nage 45 \nsport soccer
+ sed s/soccer/tennis/g
string=name sam
age 45
sport tennis
+ echo name sam age 45 sport tennis
name sam age 45 sport tennis

i would like to have the result of echo $string
Code:
name sam
age 45
sport tennis

pls guide
# 2  
Old 07-31-2012
use double quotes

Code:
 
echo "$string"

These 2 Users Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-31-2012
thanks ...it worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selectively deleting newlines with sed

I have a file that look like this: >Muestra-1 agctgcgagctgcgaccc gggttatata ggaagagacacacacaccccc >Muestra-2 agctgcg agctgcgacccgggttatataggaagagac acacacaccccc >Muestra-3 agctgcgagctgcgaccc gggttatata ggaagagacacacacaccccc I use the following sed script to remove newlines from... (2 Replies)
Discussion started by: Xterra
2 Replies

2. Shell Programming and Scripting

sed a multiple line pattern that has newlines followed by text and r

Here is the text that I was to run sed on. In this text I want to insert a semi colon ';' before 'select a13.STORE_TYPE STORE_TYPE,' and after 'from ZZMR00 pa11' Input text: insert into ZZMQ01 select pa11.STATE_NBR STATE_NBR, pa11.STORE_TYPE STORE_TYPE, ... (9 Replies)
Discussion started by: v_vineeta11
9 Replies

3. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

4. Shell Programming and Scripting

sed help required

Hi All, I have one file with below type of data in it, $ cat test.txt ###123 ###xyxytuerwb ###2 ###tyupe Here I would like to replace all the characters with "x" after the 3 "###" with the same number of characters. Can you please help me to achieve this. (7 Replies)
Discussion started by: gr8_usk
7 Replies

5. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

6. UNIX for Dummies Questions & Answers

adding symbol to newlines with out replacing them

Guyz I have been using tr command to replace symbol. I would like to add a symbol to all newlines in a textfile with out replacing them input \n (i mean new line) a \n b \n c output > a > b > c (0 Replies)
Discussion started by: repinementer
0 Replies

7. Shell Programming and Scripting

replacing strings with newlines : sed

Hi everyone, Since the previous time I received help from unix.com I have been encouraged to learn more. going through 1 of the articles(View Article) on sed I found, it pointed an interesting situation. Suppose the text is : Romeo and Ethel the Dancer Moves Audience to Tears. I... (3 Replies)
Discussion started by: hkansal
3 Replies

8. Shell Programming and Scripting

replacing comma's with newlines using sed

Hi All, silly question that I'm sure is easy to answer for a more experienced coder... I have a file called test.txt containing the following text... need, to, break, this, line, into, individual, lines using sed, I'd like to make the file look like this... need to break this line... (5 Replies)
Discussion started by: newbie_coder
5 Replies

9. Shell Programming and Scripting

Help in sed required.

Hi All, I am facing a small problem in sed. I want to insert a line in the existing file. Existing code: access to attr=userPassword by self write by * auth access to * by self write by users read by anonymous auth Desired code: access to attr=userPassword by self... (14 Replies)
Discussion started by: nua7
14 Replies

10. Shell Programming and Scripting

help required for replacing text in vi

Hi Any one pls give me command in vi to replace /opt/usr with /home/reddy/dump .... i tried escaping / with \ bt its not working... thanks in advance (6 Replies)
Discussion started by: Chandu2u
6 Replies
Login or Register to Ask a Question