473,387 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Looking for an easy way to add escape charaters to a string in BASH.

392 Expert 256MB
Hi,

I was wondering if any one knows of an easy way to add escape characters to an existing string in a BASH script.

I have a BASH script that is failing when a string is passed with brackets "[]" are passed to the sed command.

I am trying to put together a BASH script to create Unix safe file names. i.e. Removes spaces, special characters, etc.


This fails due to the brackets. It returns "/opt/content2/Transformers_The_Album/test blah[file].txt" instead of "/opt/content2/Transformers_The_Album"
Expand|Select|Wrap|Line Numbers
  1. somePath="/opt/content2/Transformers_The_Album/test blah[file].txt"
  2. highestLevel="test blah[file].txt"
  3. echo "$somePath" | sed "s/\/$highestLevel//g"
  4.  
  5. Returns :  /opt/content2/Transformers_The_Album/test blah[file].txt
  6.  
  7.  


If I manually escape the brackets "[]" then it works.

Expand|Select|Wrap|Line Numbers
  1. somePath="/opt/content2/Transformers_The_Album/test blah[file].txt"
  2. highestLevel="test blah\[file\].txt"
  3. echo "$somePath" | sed "s/\/$highestLevel//g"
  4.  
  5. Returns :  /opt/content2/Transformers_The_Album
  6.  


I was wondering if anyone knew of an command, or other simple way to add the needed escape characters to a string. I was hoping to avoid having to create all of the logic to parse the string and add the characters as needed.
Apr 13 '08 #1
1 4419
Check the printf q option:

Expand|Select|Wrap|Line Numbers
  1. $ printf "%s" "$somePath" | sed "s_$(printf "%q" "$highestLevel")__g"
  2. /opt/content2/Transformers_The_Album/
And perhaps you don't need sed:

Expand|Select|Wrap|Line Numbers
  1. $ printf "%s\n" "${somePath%/*}/"
  2. /opt/content2/Transformers_The_Album/
Or (if you need to be specific):

Expand|Select|Wrap|Line Numbers
  1. $ printf "%s\n" "${somePath/$highestLevel}"
  2. /opt/content2/Transformers_The_Album/
Apr 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Magix | last post by:
Hi, let say I have an int array, how can I put them together to construct a string? E.g int data == 77 int data == 121 int data == 32 int data == 84 int data == 101 int data == 115
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
4
by: Guadala Harry | last post by:
I need to place the following into a string... How can I properly escape the % " / < and > characters? <table width="100%" border="0" cellspacing="0" cellpadding="4px" class="hfAll"></Table> ...
3
by: Guadala Harry | last post by:
I'd like to know the answer to the following question so I can know what to expect with regard to other similar uses of escape characters and strings. While everything works fine - I'd like to know...
0
by: Mike Cooper | last post by:
Hi everyone, I am accessing several binary (PCL) files sequentially using a for loop. For each file I am using the fileget() command to populate the contents of the file into a string. I use...
16
by: DBC User | last post by:
Hi, I have a small XML file, I uploaded to a web page. I have the following code to convert the content I downloaded from web to xml and is giving "Illegal characters in path", but when I try to...
9
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
1
by: Michael Torrie | last post by:
Recently a post that mentioned a recipe that extended subprocess to allow killable processes caused me to do some thinking. Some of my larger bash scripts are starting to become a bit unwieldy...
5
by: John Ztwin | last post by:
Hello, I have a file that contains ordinary text and some special charaters in Unicode escape sequences (\uxxxx). When I read the file using e.g. StreamReader Unicode escape sequences are not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.