Question

I would like to match multiple file extensions passed through a pipe using sed and regex.

The following works:

sed '/.\(rb\)\$/!d'

But if I want to allow multiple file extensions, the following does not work.

sed '/.\(rb\|js\)\$/!d'
sed '/.\(rb|js\)\$/!d'
sed '/.(rb|js)\$/!d'

Any ideas on how to do either/or inner groups?

Here is the whole block of code:

#!/bin/sh
files=`git diff-index --check --cached $against | # Find all changed files
       sed '/.\(rb\|js\)\$/!d'                  | # Only process .rb and .js files
       uniq`                                      # Remove duplicate files
Was it helpful?

Solution

I am using a Mac OSX 10.8.3 and the previous answer does not work for me, but this does:

sed -E '/\.(rb|js)$/!d'  

Note: use -E to

Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).

and this enables the OR function |; other versions seem to want the -r flag to enable extended regular expressions. Note that the initial . must be escaped and the trailing $ must not be.

OTHER TIPS

Try something like this:

sed '/\.\(rb\|js\)$/!d'

or if you have then use -r option to use extended regular expression for avoiding escaping special character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top