sed strange quotes behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed strange quotes behavior
# 1  
Old 03-17-2010
sed strange quotes behavior

Hi gurus

input file:
Code:
1
2
3
4

desired output
Code:
1
2\
	2a
3
4

I tried
Code:
sed '2 s/$/\\\n\t2a/' files

everything works OK

but if i want to use variable instead of fixed line (2nd in this case), i have to replace ' witch " because of replacing bash variables, so I tried.

Code:
var=2
sed "$var s/$/\\\n\t2a/" files

but returns me:

Code:
1
2\n	2a
3
4
5

Could someone please explain it ? and post correct solution ? Thanks
# 2  
Old 03-17-2010
Try:
Code:
sed "$var"'s/$/\\\n\t2a/' infile

# 3  
Old 03-17-2010
Try one of these commands:
Code:
sed "$var"'s/$/\\\n\t2a/'

or
Code:
sed "$var s/$/\\\\\n\t2a/"

The shell interpretes one of the backslash as an escape character if you use double quotes.
Try to echo the sed command to see what happens:

Code:
$ var=2
$ echo "$var s/$/\\\n\t2a/"
2 s/$/\\n\t2a/          <- missing one backslash

and now with the above commands:
Code:
$ echo "$var s/$/\\\\\n\t2a/"
2 s/$/\\\n\t2a/
$
$ echo $var's/$/\\\n\t2a/'
2s/$/\\\n\t2a/

# 4  
Old 03-17-2010
Thanks both, now it is clear Smilie

---------- Post updated at 09:28 PM ---------- Previous update was at 06:59 PM ----------

Just one question

those 2 command give the same output (I added -e to echo for interpreting special characters)
Code:
echo -e "s/$/\\n/"
echo -e "s/$/\n/"

why in the first is not new line escaped ? If I want this I have to type like
Code:
echo -e "s/$/\\\n/"

Only thing which gives me sence is if first \ escapes second, so second \ is written and it escapes \n

But also this gives me the same result as previous and this gives me no sence
Code:
echo -e "s/$/\\\\n/"

I assumed first escapes second (so far second \ is written because is escaped by first \), written \ (the second) escapes third \ (so third is also written) which escapes \n.
so my expected result is: s/$/\\\n/ and NOT s/$/\n/
Can someone explain that ?

I am asking because I have traouble write something like this into the script
Code:
sed "$res""s/$/\\\\\n"$name"\\\\\n"$name"\\\/

if i type it directly to bash it runs with no problems

Thanks a lot
# 5  
Old 03-19-2010
please how to write this sed to bash script ?
# 6  
Old 03-20-2010
Quote:
Originally Posted by wakatana
I am asking because I have traouble write something like this into the script
Code:
sed "$res""s/$/\\\\\n"$name"\\\\\n"$name"\\\/

if i type it directly to bash it runs with no problems

Thanks a lot
What are the contents of the variables $res and $name?
Give a sample of some lines of the input file and the desired output with the command above.
# 7  
Old 03-21-2010
Hi $res is number of line e.g 4
and $name is username e.g tux123

Now it finnaly works Smilie I have mistake somewhere in quotes in script, also could you please explain those escaping sequences ?

I am a little confusing with escaping (maybe it is for another post, cause it is bash and not sed)

why following code gives the same result ? I assuming that in 1st case first \ escapes \n so output should be first\nsecond
but it is
first
second

Code:
1. echo -e "first\\nsecond"
2. echo -e "first\nsecond"

also folowing gives the same result
Code:
echo -e "s/$/\\\n/"
echo -e "s/$/\\\\n/"

Also I was trying eval, but did not work, why ? It is the same string echoed and then exected.
Code:
eval `echo -e "cat grp | sed "$res""s/$/\\\\\n\t\t(-,"$name",time2change.co.uk)\\\\\n\t\t(-,"$name",time2loose.co.uk)/""`

Could you please explain that or point me to some literature ? Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strange Ctrl+C behavior

Hello All, I have a strange issue. I've created a shell script which connects to RMAN (Oracle Recovery Manager) and executes full DB backup. I then executed this script with nohup and in the background: $ nohup my_script.sh > logfile.log 2>&1 &The issue is that when I tried to take a look into... (6 Replies)
Discussion started by: JackK
6 Replies

2. Shell Programming and Scripting

Strange behavior of grep

Hi All, I am facing a strange problem while grepping for a process. Here is the small script that i have written. It will look for any process running with the parameter passed to the script. If no process is running it should print appropriate message. $ cat t.ksh #!/bin/ksh set -x ... (9 Replies)
Discussion started by: veeresh_15
9 Replies

3. AIX

Strange behavior with tar

I am trying to create an archive using tar. I am specifying a list of directories using the -L option. For testing purposes I created a simple directory structure: /backup/test /backup/test/test1 /backup/test/test2 The file specified by the -L option, named files.txt, contains:... (8 Replies)
Discussion started by: judykstra
8 Replies

4. Shell Programming and Scripting

Strange behavior on one of my server

I am not sure what is wrong, but I have some strange behavior when printing things out. I do create a file with only one word test, no space, no new line etc. nano file<enter> test<ctrl x>y<enter> Server 1 gets (fail) awk '{print "+"$0"*"}' file *test Server 2 gets (OK) awk '{print... (9 Replies)
Discussion started by: Jotne
9 Replies

5. Red Hat

strange mail behavior

Hi I have script to to take backup and send mail to a group once a day. One strange behavior I have observed recently is that most of the time the mail we receive is fine . But someday it just sends out mail without any subject with undisclosed recipients. I dont know how to find the cause... (0 Replies)
Discussion started by: ningy
0 Replies

6. Programming

Strange behavior in C++

I have the following program: int main(int argc, char** argv){ unsigned long int mean=0; for(int i=1;i<10;i++){ mean+=poisson(12); cout<<mean<<endl; } cout<<"Sum of poisson: "<< mean; return 0; } when I run it, I get the... (4 Replies)
Discussion started by: santiagorf
4 Replies

7. Shell Programming and Scripting

nawk strange behavior

Dear guys; when deleting repeated lines using nawk as below ; Why the below syntax works? nawk ' !a++' infile > outfile and when using the other below syntax the nawk doesn't work? nawk ' { !a++ } ' infile > outfile or nawk ' { !a++ } ' infile > outfile BR (4 Replies)
Discussion started by: ahmad.diab
4 Replies

8. Shell Programming and Scripting

Very Strange Behavior for redirection

I have searched far and wide for an explanation for some odd behavior for output redirection and haven't come up with anything. A co-worker was working on old scripts which have run for years and embedded in their code were output redirects which worked for the script during execution and then... (5 Replies)
Discussion started by: cahook
5 Replies

9. UNIX for Dummies Questions & Answers

strange sed behavior

I have a file called products.kp which contains, for example, 12345678,1^M 87654321,2^M 13579123,3 when I run the command cat products.kp| sed -f kp.sed where kp.sed contains s,^M,, I get the output 12345678,1 87654321,2 13579123,3 (5 Replies)
Discussion started by: Kevin Pryke
5 Replies
Login or Register to Ask a Question