In sed, can I prevent \S from matching parenthesis?

Viewed 81

The input file has this content

you have to identify (http://third-view.herokuapp.com/) 

and I want to remove the URL, the desired output being

you have to identify ()

but the Sed command below also removes ')' parenthesis.

sed -i -e 's!http[s]\?://\S*!!g' file
3 Answers

Add () to the list of characters you want to terminate on, by adding them to an explicit character class:

sed -r -i -e 's!http[s]?://[^[:space:]()]*!!g' file

1st solution: Considering you want to neglect text inside (..) if yes then following may help you here.

awk 'match($0,/\(.*\)/){print substr($0,1,RSTART) substr($0,RSTART+RLENGTH-1)}' Input_file

2nd solution: In case you want to specifically substitute http or https value to just before ) then following may also help you.

awk '{sub(/http[s]?[^)]*/,"")} 1' Input_file

3rd solution: perl solution with non-greedy matching, so in case you have multiple brackets (....) and you want to substitute value only in very 1st one then try following.

perl -pe 's/\(.*?\)/()/' Input_file

This might work for you (GNU sed):

sed 's/([^)]*)/()/' file

Remove a starting paren and everything up to and including a closing paren with an opening and a closing paren.

Related