regexbashfswatch

Regex with fswatch - Exclude files not ending with ".txt"


For a list of files, I'd like to match the ones not ending with .txt. I am currently using this expression:

.*(txt$)|(html\.txt$)

This expression will match everything ending in .txt, but I'd like it to do the opposite.


Should match:

happiness.html
joy.png
fear.src

Should not match:

madness.html.txt
excitement.txt

I'd like to get this so I can use it in pair with fswatch:

fswatch -0 -e 'regex here' . | xargs -0 -n 1 -I {} echo "{} has been changed"

The problem is it doesn't seem to work.

PS: I use the tag bash instead of fswatch because I don't have enough reputation points to create it. Sorry!


Solution

  • Since question has been tagged as bash, lookaheads may not be supported (except grep -P), here is one grep solution that doesn't need lookaheads:

    grep -v '\.txt$' file
    happiness.html
    joy.png
    fear.src
    

    EDIT: You can use this xargs command to avoid matching *.txt files:

    xargs -0 -n 1 -I {} bash -c '[[ "{}" == *".txt" ]] && echo "{} has been changed"'