The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert Text at begining of file netwolf High Level Programming 3 05-27-2008 12:20 PM
How to insert text in the middle of a file kartikkumar84@g UNIX for Dummies Questions & Answers 6 05-10-2008 02:35 PM
insert some text to a file log bucci Shell Programming and Scripting 4 05-09-2007 07:19 AM
insert text into top of file jimbob Shell Programming and Scripting 1 09-22-2006 05:46 PM
insert text into a dinamic file sunbird UNIX for Dummies Questions & Answers 2 11-05-2001 10:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
I had a play with this and it's all about the quotes/double quotes to make the variable expand correctly. This works in bash...

FILENAME=input_file
NEWFILENAME=output_file
sed '$a\
PR_DbConfigEnd '"${FILENAME}"' ' $FILENAME > $NEWFILENAME

Notice to make the variable expand I used '"${FILENAME}"'

That's single, double ${FILENAME} double, single.

Try that.
  #2 (permalink)  
Old 07-02-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Another solution is to escape the dollar sign:
sed "\$a\\
  #3 (permalink)  
Old 07-02-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Hos does this work..the double backslash. I've tried numerous combinations in bash, ksh and sh. But still the only way was as i suggested above. i couldn't get it working with the \\.

Can you post the rest...i.e so I can see how the quotes etc should work around the variable in the sed text.

i.e.

FILENAME=input_file
NEWFILENAME=output_file
sed '$a\
PR_DbConfigEnd '"${FILENAME}"' ' $FILENAME > $NEWFILENAME
  #4 (permalink)  
Old 07-02-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
The trouble with posting a complete solution is that I don't exactly understand the problem completely. But here goes:
Code:
#! /usr/bin/ksh
FILENAME=input_file
NEWFILENAME=output_file
sed -e "\$a\\
PR_DbConfigEnd ${FILENAME}" <  $FILENAME > $NEWFILENAME
exit 0
I can only hope that there is some good reason for the filename to be appear inside the quotes. This will cause it to be part of the data inserted into the file. That really looks like a bug to me. If so, just delete that part. But for better or worse, I am copying from the other examples in this thread.

As for how it works, we need sed to see a line like:
$a\
but when we use double quotes, ksh processes $a as if it were a variable. But inside double quotes, a backslash removes any special power the next character has, so the \$a becomes just $a and ksh moves on. To actually get a backslash, we need \\. The first backslash is eaten by ksh as it processes the literal and this makes it leave the next backslash alone.

And bash works the same way.
  #5 (permalink)  
Old 07-02-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Got that now..I wasn't getting the backslash before the $a.

So it is working. The only reason for putting the filename in the quotes as I did was that it was the only way I could get the end result to work. So Both actually acheive teh same result now..although I coulnn't explain why mine does! your on the other hand I do understand.
  #6 (permalink)  
Old 07-03-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Quote:
Originally posted by peter.herlihy
So Both actually acheive teh same result now..although I coulnn't explain why mine does!
Well then, I'll take a crack at explaining your solution to you.

Inside single quotes, what you see is what you get. No character has any special meaning inside single quotes. So variables won't expand. A string like:
'$a'
will just be $a. The shell will not try to substitute the value of a variable called a. So in the line:
sed '$a\
we get exactly what we ask for. Inside single quotes, even the backslash is just another character. (And, btw, this means that there is no way to get a single quote inside single quotes...so 'you can't do that' won't work and there is no way to fix it with backslashes.)

When you want to use a variable inside single quotes, it won't work:
'something $var something'
isn't going to do it. So we must break that single quoted string into two single quoted string and put the variable between them:
'something '$var' something'
is actually good enough, but most folks will add {} whenever a variable touches non-blanks, not just other letters:
'something '${var}' something'

And finally, if the value of var has a string of blanks, we would need to protect them, so we would need single quotes around the variable:
'something '"${var}"' something'
And this get us to the syntax that you used. Your first single quoted string started on one line and finished on the next but otherwise it's pretty much what we have here.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0