문제

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