New line in Echo command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers New line in Echo command
# 8  
Old 11-01-2013
Quote:
Originally Posted by scott_cog
And yes i understand that because of the <br/> only my sed is not functioning.
Well, there is a basic technique you might want to learn: escaping. That means: in some programs (here: sed, but the same goes for the shell too) certain characters (here: the "/") have a special meaning. We need a way to have them treated as normal characters, with their special meaning removed, sometimes.

If you want to use "/" as part of the search- and/or replacement string you will have to solve this, because changing i.e. "bla/blub" to "something/else" is not going to work this way. sed simply won't know what is part of the command and what is part of the strings to work on:

Code:
sed 's/bla/blub/something/else/g'

Fortunately there is a device - in fact two of them - you can use. The first is to change seds special character: every character following the "s" command will be used, the only requirement is you have to use it consistently. To use "/" as a delimiter is just convention, not part of the syntax. The following two lines are equivalent and will both work:

Code:
sed 's_bla/blub_something/else_g'
sed 's!bla/blub!something/else!g'

The problem with this is that it shifts the problem only around: now you replaced "/" with another character you will have to worry about. Here is the definitive solution for that, more work, but final: you prepend the character in question with a "\". This will tell sed to not interpret the following character but take it literally:

Code:
sed 's/bla\/blub/something\/else/g'

Notice, that this works with "\" too - a "\\" will be interpreted as a literal backslash! You can even automatically "sedify" your variables before feeding them to sed. Try the following:

Code:
search="bla/blub"
replace="some/thing"

# naive way - will NOT work:

sed 's/'"${search}"'/'"${replace}"'/' /some/file > /some/otherfile

# but this works:

search="$(echo $search | sed 's/\//\\&/')"
replace="$(echo $replace | sed 's/\//\\&/')"
sed 's/'"${search}"'/'"${replace}"'/' /some/file > /some/otherfile

The application of this is left as an exercise to the interested reader. ;-))

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 9  
Old 11-01-2013
sed is probably failing because $Var_name contains newlines.
# 10  
Old 11-01-2013
Hi bakunin, that was an neat and clear explanation, really appreciate your help.

But am not that good in sed, so some how i have manged to understand whats the sed is doing in ur command.

But, still am getting error that it cannot be parsed to sed. Smilie .

Based on the If loop,
Code:
Var_name="This is first line.<br />This is second line.<br />This is third line."

sed: Function s/searchstring/This is first line.<br />This is second line.<br />This is third line./g cannot be parsed.

Modification as per your suggestion :
Code:
search="search_string"
Var_name="This is first line.<br />This is second line.<br />This is third line."
search="$(echo $search | sed 's/\//\\&/')"
replace="$(echo "$Var_name" | sed 's/\//\\&/')"
sed 's/'"${search}"'/'"${replace}"'/g' | sed 's/search/replace/g' > tmp_file

Please let me know what mistake am doing ?

---------- Post updated at 08:18 AM ---------- Previous update was at 07:22 AM ----------

Thanks Scrutinizer, That helps and fixed the issue as well perfectly. But am not able to find your post online, i got the mail, but am not able to find it here. I really want to thank you.
It may sound little weird, but am not able to see your reply.

I really appreciate the help from Scott and
Scrutinizer.


Last edited by scott_cog; 11-01-2013 at 10:09 AM..
# 11  
Old 11-01-2013
Quote:
Originally Posted by scott_cog
It may sound little weird, but am not able to see your reply.
Was it a private message? They're at top-right.
# 12  
Old 11-01-2013
Hi CarloM,

Thanks for your reply, no i chked over there as well, its not visible.

Anyway, am posting the response from Scrutinizer here,


*******************
This is probably failing because $Var_name contains forward slashes. Try using s|...|| instead of s/.../../
******************

This solves the problem, thanks to Scrutinizer for giving this.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

2. Shell Programming and Scripting

Need to echo command successful if command is executed successfully

Hello, I have written a command n shell script : srvctl relocate service -d t1 -s s1 -i i1 -t t1 -f If the above command executes successfully without error I need to echo "Service relocated successfully and If it errors out I need to trap the errors in a file and also need to make... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

3. Shell Programming and Scripting

How to read in a part of an echo line?

Hello, I have a very basic script #!/usr/bin/ksh while print ' ' print ' 1. View Command History ' print ' 2. List files in current Directory ' read opt'?Enter Option> ' ;do if ;then fc -l fi # if ;then ls -la I want to... (10 Replies)
Discussion started by: Grueben
10 Replies

4. Shell Programming and Scripting

echo two command output in the same line

Hi, I try to write script and echo two command at the same line . echo "A" echo "B" How can I pipe above two command at the same line in text file . So, in the output text file , you can see below ??? A B not A B Any sugggestion ??? (4 Replies)
Discussion started by: chuikingman
4 Replies

5. Shell Programming and Scripting

Echo and a command's output on the same line

Hello, I'm writing some bash scripts and I'm trying to get an echo command and the output of another command to display on the same line. For example: I want to run echo "Operating System: " unameand have it displayed as Operating System: Darwin Thanks for your help! (7 Replies)
Discussion started by: codyhazelwood
7 Replies

6. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

7. Shell Programming and Scripting

new line in echo

Hi i would like disply the new line in echo command. i have input like: echo " X1 02:12:13 X2 03:02:12 " out put: X1 02:12:13 X2 03:02:12 can you tell how can use new line option in echo command. (5 Replies)
Discussion started by: koti_rama
5 Replies

8. UNIX for Advanced & Expert Users

echo and new line

hi I want to display: The students are: a b c What I get from echo "The students are:" "$list" is The students are: a b c any idea? (6 Replies)
Discussion started by: melanie_pfefer
6 Replies

9. Shell Programming and Scripting

Output of both the echo statement in one line

I have script like echo -n FINISHED FEXP: ${TABLE2EXP} echo $STATUS I want the output of both the echo statement in one line How can i do this (3 Replies)
Discussion started by: scorp_rahul23
3 Replies

10. Shell Programming and Scripting

how to echo the file contents LINE BY LINE

hello, i have a listing (let say ABC) consists of the below: : public database link public synonym role rollback segment : when i run the below for loop, for i in `more ABC` do echo "$i" done it gives me, : public database (4 Replies)
Discussion started by: newbie168
4 Replies
Login or Register to Ask a Question