Remove bracket part


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove bracket part
# 8  
Old 07-29-2012
@alister can you please change in the code I posted so it will be correct?
Code:
 echo 'hgfd[xtd]   123' | sed -E 's/\[([^]]*)\]/\1/'

then I will change in my earlier post so I don't mislead somebody.
# 9  
Old 07-30-2012
Quote:
Originally Posted by 244an
[^]]* that means all characters but "]" zero or more times.
\[[^]]*\] on "[brackettext1] some other text [brackettext2] some different text"
will find "[brackettext1]"
but \[.*\]
will find "[brackettext1] some other text [brackettext2]"

And to keep the bracket text:
Code:
 echo 'hgfd[xtd]   123' | sed -E 's/\[([^]]*)\]/\1/'


I know that [ expression ] in regexp means that: match the expression .
And the ^ in expression means "not include".

My question is:
1. If Metacharacter appears within [ expression ] , which means the character itself, for example, in \[[^]]*\], why does it not neccessary need to be escaped? Is it a common rule?
2. echo 'hgfd[xtd] 123' | sed -E 's/\[([^]]*)\]/\1/', what does -E means?
3. If I want to delete all the "[" and "]" like this: just match "[" or "]" and replace it with "", the code like sed s; [ [ ] ] ;; . It doesn't work. how to modify?
To put it simple.
Source string: abc[[4545[]]]adfd[[[[asfdsfsf[][[[][][asdf. I want to delete all the "[" and "]"
4. vgersh99's code.echo 'hgfd[xtd] 123' | sed 's/\[[^]]*\]//1', what does the last number: 1 means?
Thanks a lot.
# 10  
Old 07-30-2012
Quote:
Originally Posted by painless
I know that [ expression ] in regexp means that: match the expression .
And the ^ in expression means "not include".

My question is:
1. If Metacharacter appears within [ expression ] , which means the character itself, for example, in \[[^]]*\], why does it not neccessary need to be escaped? Is it a common rule?
because sed knows how to deal with 'nested' '[]'.
Quote:
Originally Posted by painless
2. echo 'hgfd[xtd] 123' | sed -E 's/\[([^]]*)\]/\1/', what does -E means?
'-E' - is for Extended Regex (in some sed implementations.
Quote:
Originally Posted by painless
3. If I want to delete all the "[" and "]" like this: just match "[" or "]" and replace it with "", the code like sed s; [ [ ] ] ;; . It doesn't work. how to modify?
To put it simple.
Source string: abc[[4545[]]]adfd[[[[asfdsfsf[][[[][][asdf. I want to delete all the "[" and "]"
echo ' abc[[4545[]]]adfd[[[[asfdsfsf[][[[][][asdf' | sed 's/[][]//g'
Quote:
Originally Posted by painless
4. vgersh99's code.echo 'hgfd[xtd] 123' | sed 's/\[[^]]*\]//1', what does the last number: 1 means?
'1' is for the FIRST regex match
Quote:
Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

How to remove contents from file which are under bracket?

hello Friend, In hostgroup file, i have define lots of hostgroups. I need to remove few of them without manually editing file. Need script or syntax. I want to search particular on hostgroup_members and delete hostgoup defination of it. for example. define hostgroup{ hostgroup_name... (8 Replies)
Discussion started by: ghpradeep
8 Replies

3. Shell Programming and Scripting

Remove part of the file using a condition

Gents, Is there the chance to remove part of the file, Taking in consideration this condition. For each record the first row start with the string % VE should be 56 rows for each records.. first row = % VE last row = % sw total 56 rows for each record. Then in the case that the... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

Remove bracket part entires and separate entries after comma

Hi all This time my input conatin 3 columns: ERCC1 (PA155) Platinum compounds (PA164713176) Allele A is not associated with response to Platinum compounds in women with Ovarian Neoplasms as compared to allele C . CES1 (PA107) methylphenidate (PA450464) Genotype CT is not... (4 Replies)
Discussion started by: Priyanka Chopra
4 Replies

5. 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

6. Shell Programming and Scripting

Remove part of a string from second field

I have a file in below format (pipe delimited): 1234__abc|John__abc|xyz 3345__abc|Kate__abc|xyz 55344|Linda__abc|xyz 33434|Murray|xyz I want to remove any occurence of "__abc" in the second field of this file. I did some research and found a way to replace the entire second field with... (5 Replies)
Discussion started by: rajv
5 Replies

7. Shell Programming and Scripting

remove part of a line

Hi I need some help with using shell script to analyze the content of a file. I hope someone can help me. I have a file with content like the following: /foldera/database/procedure/a.proc$$/version1/2 /folderb/database/procedure/proj1/b.proc$$/version2/2 I need to write a shell script to... (16 Replies)
Discussion started by: tiger99
16 Replies

8. Shell Programming and Scripting

Remove part of the file

I have an xml file, from where I need to take out Application2 entries and keep the others. I need to remove from <product> to </product> and the key element to look for while removing should be <application> as other pairs can be same for others. <product> ... (10 Replies)
Discussion started by: chiru_h
10 Replies

9. Shell Programming and Scripting

remove certain part of file name

Hi, Is it possible to remove the first part of the file name using find. i.e i have something like 2006abc.txt , 1007bed.txt etc, I wanna rename them to abc.txt , bed.txt I tried some stupid way.. find . -name '*.txt' -exec mv {} `cut -f2-5 -d"_" {}` \; somehow iam not getting it. ... (3 Replies)
Discussion started by: braindrain
3 Replies
Login or Register to Ask a Question