sed replace word after a character

时间:2018-02-03 10:06:55

标签: regex sed

I would like to find in a file all word follow by slash '/' and replace them by empty

Example:

I/a want/s to/as remove/po word/sa after/al slash/q ./.
-> I want to remove word after slash .

I thought about this command but it doesn't work:

sed 's/(\/\w+)/g' inputFile.txt > outputFile.txt

Does any one have a suggestion ? Thank you in advance.

EDIT: Both the solutions of Yudong and Choroba work well, thank you.

For more general, I think about finding characters after slash until reaching a space with the pattern:

\/[^ ]+

But it doesn't work in sed 's/\/[^\ ]+//g'

Does any one help me to explain it ?

2 个答案:

答案 0 :(得分:3)

You can use the \B boundary, it matches inside "words":

sed 's=/\(.\B\)*.==g'

Note that I used = as the delimiters to avoid the need to backslash the slash.

答案 1 :(得分:2)

echo 'I/a want/s to/as remove/po word/sa after/al slash/q ./.' | sed 's/\/[a-zA-Z.]*//g'
I want to remove word after slash .

's/(\/\w+)/g' you missed a replace section, sed replace should be like: sed 's/regexp/replacement/' .