質問

I was wondering if there was a simple way to add exceptions to the [[:punct:]] bracket expression when using tr utility:

cat *.txt | tr '[[:punct:]]' '\012'

for instance: do not do anything if the punctuation characters are - or ).

役に立ちましたか?

解決

You can use a negative look-ahead: '(?![-)])[[:punct:]]'

This will first check whether the next character is neither a -nor a ), and then check it for whether it's a punctuation character. Using a negative look-behind is also possible and might or might not be faster: '[[:punct:]](?<![-)])'

edit: since tr apparently doesn't support Regex (only basic POSIX), you should use another utility, e.g. sed: cat *.txt | sed -r 's/(?![\-@\/\\¤%+[&|=^\]$_*#])[[:punct:]]/\012/g'

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top