Removing string between two particular strings in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing string between two particular strings in a line
# 1  
Old 01-06-2012
Removing string between two particular strings in a line

Hi,

I have a file with following format:

1|What is you name (full name)?|Character
2|How far [in kilometers] is your school [from home]?|Numeric

Now I need to remove everything inside brackets () or []. There can be more than one pair of brackets. The output file should look like:
Code:
1|What is you name?|Character
2|How far is your school?|Numeric

Can anybody please help?

I tried with
Code:
sed 's/\[*\]//g'

but not working.

Last edited by Franklin52; 01-06-2012 at 05:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-06-2012
Code:
perl -pe 's/ [(\[].+?[)\]]//g' inputfile

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-06-2012
You were close. Sed's pattern matching is greedy though, so the regex
Code:
s/\[.*\]//g

will match everything from the first opening bracket to the last closing one. What you need is to replace the '.' (which means 'any character'), with 'not closing bracket', that is [^]]:
Code:
sed 's/\[[^]]*\]//g'

should do the trick
This User Gave Thanks to mirni For This Post:
# 4  
Old 01-06-2012
Thanks a lot! you guys are great!
The perl one is working exactly what I needed.
But for sed I am becoming fool when trying to to do it for (). Following are not working:
Code:
sed 's/(^*)//g'
sed 's/\((^))*\)//g'

Do we need to handle () in a different way, I guess we don't need to negate () like we need to do for [].

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-06-2012 at 06:18 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 01-06-2012
The difference is that '[' and ']' are metacharacters in regexp.
[^a] means all characters but not 'a'. So literal '[' or ']' needs to be escaped as \[, resp. \].

With parens its actually simpler:
Code:
sed 's/([^)]*)//g'

Since you don't have to escape them. But it's the same logic -- you do use the [^)] to match all characters but not ')'. If you allowed ')' to be matched, it would greedily eat the whole thing between first ( and last ). Not what you want here.
# 6  
Old 01-06-2012
Awesome! Thank you!
# 7  
Old 01-06-2012
Quote:
Originally Posted by mirni
You were close. Sed's pattern matching is greedy though, so the regex
Code:
s/\[.*\]//g

will match everything from the first opening bracket to the last closing one. What you need is to replace the '.' (which means 'any character'), with 'not closing bracket', that is [^]]:
Code:
sed 's/\[[^]]*\]//g'

should do the trick
In fact the last closing bracket does not need to be escaped, it will already be taken as litteral :
Code:
...  sed 's/\[[^]]*]//g'

---------- Post updated at 04:05 PM ---------- Previous update was at 03:56 PM ----------

The right expression to do it one shot is :

Code:
... sed 's/[[(][^])]*[])]//g'

Code:
$ echo 'this (is) a [test]!!!'
this (is) a [test]!!!
$ echo 'this (is) a [test]!!!' | sed 's/[[(][^])]*[])]//g'
this  a !!!

Note that this one shot notation will also match the [...) and (...] blocks :
Code:
$ echo 'this (is) a [darn) funny (and] crazy [test]!!!'
this (is) a [darn) funny (and] crazy [test]!!!
$ echo 'this (is) a [darn) funny (and] crazy [test]!!!' | sed 's/[[(][^])]*[])]//g'
this  a  funny  crazy !!!


Last edited by ctsgnb; 01-06-2012 at 11:11 AM..
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching some string in a line and removing that

I have one output file. Node: hstg1so Date: 2013/07/16 17:51:24 GMT Totals: 10608 6871 0 2208 1529 0 0 64% 0% ( 0 ) Node: hstg2so Date: 2013/07/16 17:51:25 GMT Totals: ... (3 Replies)
Discussion started by: Raza Ali
3 Replies

2. Shell Programming and Scripting

Removing command line arguments from string list

I am passing a list of strings $list and want to remove all entries with --shift=number, --sort=number/number/..., --group=number/number/... Also are removed whether upper or lower case letters are used For example the following will all be deleted from the list --shift=12 --shift=2324... (7 Replies)
Discussion started by: kristinu
7 Replies

3. Shell Programming and Scripting

script in perl for removing strings between a file

I have file that looks like: ATOM 2517 O VAL 160 8.337 12.679 -2.487 ATOM 2518 OXT VAL 160 7.646 12.461 -0.386 TER ATOM 2519 N VAL 161 -14.431 5.789 -25.371 ATOM 2520 H1 VAL 161 -15.336 5.698 -25.811 ATOM 2521 H2 VAL 161 -13.416 10.529 17.708 ATOM 2522 H3 VAL 161 -14.363 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

4. Shell Programming and Scripting

Removing a line IF the next line contains string

So, I've been working on a project which takes layer 7 metadata from pcap dumps and archives it. However, there is a lot of dataless information that I don't want in my output. I know of ways to produce the output I want from the input file below, but I want a method of doing this, regardless of... (2 Replies)
Discussion started by: eh3civic
2 Replies

5. SuSE

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

6. Shell Programming and Scripting

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

7. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

Perl removing strings from a variable value

Dear all, I have a variable called $abc, which the value is something like below, *** *********** : ***** where * can be anything. I need to remove all but the final characters until last whitespace. example grd groupstudy : tutor6/7 becomes tutor6/7 something like if... (2 Replies)
Discussion started by: tententen
2 Replies

9. Shell Programming and Scripting

Removing text between two static strings

Hi everyone, I need to replace the text between two strings (html tags) and I'm having trouble figuring out how to do so. I can display the text with sed but I'm not having any luck deleting the text between the two strings. My file looks like this: <oths>test</oths><div class="text">1928... (2 Replies)
Discussion started by: cg2
2 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question