Domanda

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 ).

È stato utile?

Soluzione

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'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top