Remove bracket part


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove bracket part
# 1  
Old 07-28-2012
Remove bracket part

Hi

I have to remove in a file in first column whatever is written in brackets with brackets [ xyz]

so one file
Code:
hgfd[xtd]   123
gfhdj[dfg]  483
jdgfdg[urtr]  34738

the output shuld be

Code:
hgfd    123
gfhdj   483
jdgfdg  34738

# 2  
Old 07-28-2012
Code:
echo 'hgfd[xtd]   123' | sed 's/\[[^]]*\]//1'

# 3  
Old 07-28-2012
use sed:
Code:
sed 's/\[.*\]//' filename


Last edited by Franklin52; 07-28-2012 at 03:36 PM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 07-28-2012
Quote:
Originally Posted by painless
use sed:

sed 's/\[.*\]//' filename
Be careful with something like this. While it didn't appear in the small sample posted, if the input line contained something like

Code:
foo[bar] some other text[3]

The sed here would leave only 'foo' in the record. Remember, regex is a greedy match and \[.*\] will match from the left most bracket through the last bracket on the line. The expression that vgersh99 posted does not fall victim to this as there is cleverness coded that prevents the match from extending past the first closing bracket.
This User Gave Thanks to agama For This Post:
# 5  
Old 07-29-2012
Quote:
Originally Posted by vgersh99
Code:
echo 'hgfd[xtd]   123' | sed 's/\[[^]]*\]//1'

Would you please explain more about this? I have added some blank for read convenience.

Code:
sed 's  /   \[  [^]]  *   \]   //1  '

---------- Post updated at 02:31 AM ---------- Previous update was at 02:27 AM ----------

Quote:
Originally Posted by painless
Would you please explain more about this? I have added some blank for read convenience.

Code:
sed 's  /   \[  [^]]  *   \]   //1  '

And by the way, if I want to delete the bracket pair with sed, leaving the content within the bracket.
How?
Thank you in advance.
# 6  
Old 07-29-2012
[^]]* 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/'


Last edited by 244an; 07-29-2012 at 11:28 AM..
# 7  
Old 07-29-2012
Outside of a bracket expression, ] does not need to be escaped. It is not special.

In \[[^]]*\] and \[.*\], according to POSIX, \] is an undefined sequence. Most regular expression implementations will throw away the backslash, but they aren't required to. It would be compliant to abort with a syntax error.

Within a bracket expression, where backslash and other special characters lose their special meaning, \] simply means a literal backslash is the final character in the bracket expression.

Regards,
Alister

Last edited by alister; 07-29-2012 at 12:34 PM..
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