Need help to escape special characters in Korn shell script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Need help to escape special characters in Korn shell script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2007
rogers42 rogers42 is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 20 December 2011, 3:55 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Need help to escape special characters in Korn shell script

Hi,

I would like to display the following message from my shell (Korn) script

Copy "old_file.txt" to "new_file.txt"

My code looks as follows

print "Copy "old_file.txt" to "new_file.txt""

However, when I execute the script, I get the following output

Copy old_file.txt to new_file.txt

Can somebody please help me with the syntax to escape the double quotes (") ?

Thanks in advance.

rogers42
Sponsored Links
    #2  
Old 12-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 November 2014, 10:48 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts

Code:
$ print "Copy \"old_file.txt\" to \"new_file.txt\""
Copy "old_file.txt" to "new_file.txt"
$

Sponsored Links
    #3  
Old 12-29-2007
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
 
Join Date: Nov 2001
Last Activity: 23 October 2011, 10:07 PM EDT
Location: Brisbane, Australia
Posts: 509
Thanks: 7
Thanked 0 Times in 0 Posts
You could have also gone with ...
Code:
[mddev:/home/cameron]
$ print 'Copy "old_file.txt" to "new_file.txt"'
Copy "old_file.txt" to "new_file.txt"

    #4  
Old 12-29-2007
rogers42 rogers42 is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 20 December 2011, 3:55 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks for the tips folks. Much appreciated.

rogers42
Sponsored Links
    #5  
Old 04-29-2008
Thinkpositiv Thinkpositiv is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 14 May 2009, 7:23 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

I also need some quoting help. I tracked my problem down in a simple example.
I have a grep that is working if typed at the shell prompt (it is important for me to have a space in the character class):

Code:
grep -E '[a-zA-Z ]{30}:' myFile

But included in a script file (lets say file 'runMe') like the following:

Code:
#!/bin/ksh
typeset pattern='[a-zA-Z ]{30}:'
echo "Command to be executed: grep -E $pattern myFile"
grep -E $pattern myFile

Executing the file 'runMe', I get the following error:
grep: RE error in '[a-zA-Z [ ] imbalance or syntax error

I googled/searched here for the error and for quoting or inserting something else which recognizes a space character, but with no success yet.
Also I tried various quotings in runMe, like "'[a-zA-Z ]{30}:'" or "\"[a-zA-Z ]{30}:\"" or quoting the space character like "[a-zA-Z ]{30}:" but all lead to the same error message. Seems like it is breaking at the space?

Curious for me:
If I execute the script and copy the line which is printed by the 'echo' statement and paste this line at the command prompt, that works! It just doesn't work within the script...

Maybe this not the adequate topic, because its more a grep issue than a quoting issue. If so, please be patient with me

FYI:
I'm relatively new to unix.
In 'myFile', I want to (simplified for this issue) match lines, that have the first 30 characters being (a letter or a space), followed by a colon.
I've aliased grep to /usr/xpg4/bin/grep (and also tried /usr/xpg4/bin/grep manually without alias).

thanks in advance for any hints, Thinkpositiv

Last edited by Thinkpositiv; 04-29-2008 at 11:32 AM..
Sponsored Links
    #6  
Old 05-02-2008
Thinkpositiv Thinkpositiv is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 14 May 2009, 7:23 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I got it myself. I missed the double quotes when using the variable (not a declaration time of the variable)!


Code:
#!/bin/ksh
typeset pattern="[a-zA-Z ]{30}:"
echo "Command to be executed: grep -E $pattern myFile"
grep -E "$pattern" myFile

Hope this helps someone
Sponsored Links
    #7  
Old 05-14-2009
Thinkpositiv Thinkpositiv is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 14 May 2009, 7:23 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

I'm back with a similar issue
Basically I'm grep'ing a file for a pattern. The pattern contains " (double quotes) and a space. So I escaped the quotes. But how do I use this variable in the grep call?

Code:
typeset searchString="path=\"asdf\" mode=\"jkl\""
typeset found=`grep "$searchString" $cs_rte_configtransform` #(a)
typeset found=`grep '$searchString' $cs_rte_configtransform` #(b)

Neither (a) nor (b) works. From my understanding I'd have to use (a), but searchString should contain path=\"asdf\", so I need to declare it like this?

Code:
typeset searchString="path=\\\"asdf\\\" mode=\\\"jkl\\\""

Still no results, the variable found is always empty, although the searchString manually pasted into the grep call works.

Has anyone an idea/hint for me?
Thanks, regards, Thinkpositiv
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with sed to escape special characters majose Shell Programming and Scripting 4 02-06-2012 05:56 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-06-2012 12:40 AM
Korn shell to insert cyrillic characters into the databse vkca Shell Programming and Scripting 3 09-26-2011 02:43 PM
Replace new line with <br /> & escape special characters ted_chou12 Shell Programming and Scripting 2 02-07-2011 09:25 PM
awk print $1 escape all special characters unclecameron Shell Programming and Scripting 6 05-29-2010 04:38 PM



All times are GMT -4. The time now is 05:57 PM.