shantanuo
shantanuo

Reputation: 32306

removing text before and after ()

How do I remove the text till the first ( and after )?

INSERT INTO `todel` VALUES (1,'akbar\'s','Mumbai, Delhi road, India');
INSERT INTO `todel` VALUES (2,'amar\"s','South Africa, ghana');

The expected output is like this...

1,'akbar\'s','Mumbai, Delhi road, India'
2,'amar\"s','South Africa, ghana'

Upvotes: 2

Views: 6345

Answers (3)

glenn jackman
glenn jackman

Reputation: 246744

awk can take a regular expression as field separator, so use either parenthesis as the field separator and just emit the 2nd field:

awk -F'[()]' '{print $2}' filename

Upvotes: 1

kurumi
kurumi

Reputation: 25599

Ruby(1.9+)

$> ruby -ne 'print $_.sub(/.*\(|\).*$/,"")' file
1,'akbar\'s','Mumbai, Delhi road, India'
2,'amar\"s','South Africa, ghana'

or the shell(bash)

$> while read -r line; do line=${line#*(}; echo ${line%)*}; done <file
1,'akbar\'s','Mumbai, Delhi road, India'
2,'amar\"s','South Africa, ghana'

or awk

$> awk '{sub(/.*\(/,"");sub(/\).*/,"")}1' file
1,'akbar\'s','Mumbai, Delhi road, India'
2,'amar\"s','South Africa, ghana'

or sed

$> sed -rn 's/.*\(//;s/\).*//p' file
1,'akbar\'s','Mumbai, Delhi road, India'
2,'amar\"s','South Africa, ghana'

Upvotes: 2

bmk
bmk

Reputation: 14137

You can remove everything from beginning of line until the first ( and from (including) the last ) till the end of line with sed:

sed -r 's/^[^(]*\(.*)\)[^)]*$/\1/'

Upvotes: 0

Related Questions