Doubt with regex to replace square bracket


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt with regex to replace square bracket
# 1  
Old 01-10-2013
Wrench Doubt with regex to replace square bracket

there is a word "welcome[world]"
output should be "welcome\[world\]

i am using regsub to add backslash "\" in place where ever i find square brackets (open or close)..
But i am not getting it... pls help out..

set a {welcome[world]}
set d [regsub -all "(?q)\[" "$a" /\[]
# 2  
Old 01-10-2013
Code:
x='welcome[world]'
echo $x | sed 's/\[\|\]/\\&/g'

# 3  
Old 01-10-2013
doubt again

thanks. can we do the same in tcl expect script.. pls help
# 4  
Old 01-10-2013
Code:
set a {welcome[world]}
regsub -all {\[} $a {\\[} a
regsub -all {\]} $a {\\]} d

puts $d

# 5  
Old 01-10-2013
can we do it in a single line???
# 6  
Old 01-10-2013
Code:
set a {welcome[world]}
regsub -all (\\\[|\\\]) $a {\\\1} a

puts $a

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Regex Expression Replace

I have a XML file where there is a tag with like <wd:address_line_1>1234 Street</wd:address_line_1> I want to replace the values "1234 Street" with "Test Data". Different people have different address lines and i want to replace with a fixed value to mask the file. I was trying to use sed... (7 Replies)
Discussion started by: dr46014
7 Replies

2. Shell Programming and Scripting

Regex - search and replace

I have file which contains data in the following format all in a single line: BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI ( OFC_ID ,CSHBX_ID ,TRXN_SEQ_NUM ,PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );... (4 Replies)
Discussion started by: ysvsr1
4 Replies

3. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

4. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

5. Shell Programming and Scripting

Doubt on RegEx

Friends, I have a silly doubt: Assume that I have a line like this Heading: Value1; SomeText1 (a, b, c), Value 2; SomeText2 (d, e, f) I wanted to remove all semicolon and remove everything in brackets (including brackets). I managed to do this using this code if... (1 Reply)
Discussion started by: dahlia84
1 Replies

6. Shell Programming and Scripting

Group on the basis of common text in the square bracket and sorting

File A 99 >ac >ss >juk 70 >acb >defa 90 >ca 100 >aa >abc >bca 85 >cde 81 >ghi >ghij 87 >def >fgh <ijk 89 >fck >ghij >kill >aa The given output shud be 100 >aa >abc >bca 87 >def >fgh <ijk 89 >fck >ghij >kill >aa (2 Replies)
Discussion started by: cdfd123
2 Replies

7. Shell Programming and Scripting

Expect Script square bracket dollar prompt

Hi Thanks for this amazing forum first, I've been searching answers in it for problems that I've encountered at work. The only problem I haven't been able to find a fix for, is a ever waiting for prompt problem in Expect when encounter a $ prompt. I usually set the timeout to -1 cause the... (2 Replies)
Discussion started by: Ikaro0
2 Replies

8. UNIX for Dummies Questions & Answers

replace line after a regex

Hi, I am trying to write a script which will modify a given account's settings by searching for a line in a file and then replacing the line after it. Here is a portion of my input file: type=friend username=0002 secret=password host=dynamic dtmfmode=rfc2833 mailbox=0002 context=sip... (2 Replies)
Discussion started by: the1armedcoder
2 Replies

9. Shell Programming and Scripting

Extract string in square bracket

Hi Input text is some message some message some message Expected output is main value1 value2 value3 Any idea how to above values in square brackets using shell scripting? many thanks. (3 Replies)
Discussion started by: hnh
3 Replies

10. Shell Programming and Scripting

doubt regardin regex in grep

shudnt this command : grep test give all the lines which do not hv 'C'. ^ wrks as negating character inside square brackets right ??? bt in my case grep is printin all the line in the file also wht does grep c+ test & grep c? test shud do ??? (4 Replies)
Discussion started by: evergreen_cool
4 Replies
Login or Register to Ask a Question