Closed federico-piazza closed 4 years ago
Or grep - possibly the most frequent command regexps arenused with.
I'd be happy to if someone could clear up the "rules" for each flavor, so I can try to make the output as nice as possible.
@firasdib As far as I know, grep and sed have parameters to support PCRE regex. In case of grep I think it is grep -P and in case of sed it is sed -r (-E in mac). Also I think there is ssed for full pcre support.
It would be very useful, it's really painful to escape the regex string for sed. Edit never mind, using single quotes solves this issue.
for sed, +1 It would be very useful.
Can anyone write the codegen for this? See https://github.com/firasdib/Regex101/wiki/Writing-a-Code-Generator
I would recommend not doing awk
and focussing solely on sed
for now. Awk is a very different beast and sed
should be installed pretty much everywhere where there is also awk
.
I would also avoid grep
for now. Whilst it at first sounds like the goto regex evaluator for the cmdline in my personal experience it has a lot of unexpected limitations when it comes to using it with extended regex.
Keep in mind that on macOS the default sed
is from BSD which has quite different behaviour and flags. Don't bother supporting that old thing. Those who really need old BSD sed
hopefully know how to use it anyway.
Instead simply use gsed
instead of sed
. It gives you the ubiquitous GNU sed with all the familiar flags and behaviour.
It can be easily installed with brew install gnu-sed
.
My go to flags for GNU sed
which allow me to basically copy paste RegEx from Regex101 are:
gsed -r 's/YOUR_REGEX_GOES_HERE/YOUR_SUBSTITUTION_GOES_HERE/FLAGS_GO_HERE;t;d'
# Examples
gsed -r '/Amperage/s/^.+\s+([-0-9]+)\s*$/\1/;t;d'
sed -r '/cd/s/.+;\s*cd\s+(.+)/\1/;t;d'
history 1 | sed 's/^ *[0-9]* *//' |
Here is what -r
does:
-E, -r, --regexp-extended
use extended regular expressions in the script (for portability use POSIX -E).
As to ;t
and ;d
: Those are sed commands that I do not dare to touch. I grandfathered these from a more seasoned sed
veteran and I have had 0 problems with them but a great many problems without them. If you care to learn more, you can do so from the GNU documentation but be warned.
I made a first draft of a sed
bash
code generator.
Please help yourself to adding proper escaping because right now it just hopes there is no '
in there.
class Sed extends PureComponent {
static propTypes = {
regex: PropTypes.string.isRequired,
flags: PropTypes.string.isRequired,
delimiter: PropTypes.string.isRequired,
testString: PropTypes.string.isRequired,
isSubstituting: PropTypes.bool.isRequired,
isGlobal: PropTypes.bool.isRequired,
substString: PropTypes.string
};
render() {
return (
<Highlight lang="sed">
{this._renderCode()}
</Highlight>
);
}
_renderCode() {
const { regex, flags, delimiter, testString, isSubstituting, substString, isGlobal } = this.props;
const codeString = new CodeString();
if (! isSubstituting) {
codeString.append("This generator only supports substitution. Please update your regular expression accordingly");
return codeString.toString();
}
codeString.append(`sed -E \'s/${regex}/${substString}/${flags};t;d\' <<< "${testString}" `);
return codeString.toString();
}
}
export default Sed;
Wow, I've totally missed this. Many thanks @phdoerfler
I'll add this to my todo list.
@guest271314 please join the chat (IRC) if you are looking for this type of help. Your question is kind of outside the scope of GitHub issues, and you commented on a long closed issue.
It would be nice to have in Code Generator the sample for sed and awk