'Convert sed command from linux to freebsd / macos

I have a very small script that someone here helped me with. It deletes all tracks that are under 1000bits. Works great under linux. Works as well under freebsd and macos but spits out a lot of error messages, saying too many arguments on the sed part, which is getting annoying. Can someone please help me converting this to work nicely under freebsd / macos? Thank you


for flacfile in *.flac; do
    [ $(mediainfo "$flacfile" | grep 'Bit rate' | grep -oi '[0-9].[0-9]*'
| sed 's/\s//g') -lt 1000 ] && rm "$flacfile"                                                                          $
done

Error message

/usr/local/bin/flac-remove: command substitution: line 6: syntax error near unexpected token `|'
/usr/local/bin/flac-remove: command substitution: line 6: `| sed 's/\s//g''
sed


Solution 1:[1]

suggestion 1

Use [[ ]] for numeric testing in bash

for flacfile in *.flac; do
    [[ $(mediainfo "$flacfile" | grep 'Bit rate' | grep -oi '[0-9].[0-9]*'
| sed 's/\s//g') -lt 1000 ]] && rm "$flacfile"                                                                          $
done

suggestion 2

Replace grep 'Bit rate' | grep -oi '[0-9].[0-9]*' | sed 's/\s//g' with single awk command

awk '/Bit rate/ && /[0-9].[0-9]*/{gsub("[[:space:]]","");print}'

So that:

for flacfile in *.flac; do
    [[ $(mediainfo "$flacfile" | awk '/Bit rate/ && /[0-9].[0-9]*/{gsub("[[:space:]]","");print}') -lt 1000 ]] && rm "$flacfile"                                                                          $
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dudi Boy